启动时出现空指针异常

时间:2013-10-09 02:10:50

标签: android

我有一个主要活动,应该在打开应用程序时加载一个小的启动xml文件2或3秒。我在oncreate中尝试了这段代码,然后将其添加到我的主要项目中。请记住,这两个应用程序单独工作但由于某种原因,我在运行应用程序时得到一个空指针异常..... help?

MainActivity:

package com.Depauw.dpuhelpdesk;

import android.net.Uri;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

private Button knowledgeBase, submitRequest, helpme, faq, technician, call;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);
    //display the logo during 5 secondes,
    new CountDownTimer(2000,1000){
        public void onTick(long millisUntilFinished){} 

        public void onFinish(){
               //set the new Content of your activity
               MainActivity.this.setContentView(R.layout.activity_main);
        }
   }.start();

    Initialize();
}

private void Initialize(){
    knowledgeBase = (Button) findViewById(R.id.knowledgebase1);
    submitRequest = (Button) findViewById(R.id.submitrequest1);
    helpme = (Button) findViewById(R.id.helpButton);
    faq = (Button) findViewById(R.id.faqButton);
    technician = (Button) findViewById(R.id.submitrequest2);
    call = (Button) findViewById(R.id.callButton);

    knowledgeBase.setOnClickListener(new OnClickListener(){
        public void onClick(View arg0){
            Intent knowledgeIntent = new Intent(MainActivity.this, knowledgebase1_activity.class);
            startActivity(knowledgeIntent);
        }
    });

    submitRequest.setOnClickListener(new OnClickListener(){
        public void onClick(View arg0){
            Intent requestIntent = new Intent(MainActivity.this, submitRequest_activity.class);
            startActivity(requestIntent);
        }
    });

    helpme.setOnClickListener(new OnClickListener(){ //dialog box
        public void onClick(View arg0){

        final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("Help");
        builder.setMessage("This app allows you to access the IT KnowledgeBase from DePauw's website." + " " +
                "If you experience any issues using our app, please send us an email to helpdesk@depauw.edu or call 765-658-4294");
        builder.setCancelable(true);
        builder.setIcon(R.drawable.ic_launcher);
        builder.setPositiveButton("Close", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int id) {
                builder.setOnCancelListener(null);
                    }
                });
            builder.create().show();  // create and show the alert dialog
        }
    });

    faq.setOnClickListener(new OnClickListener(){
        public void onClick(View arg0){
            Intent requestIntent = new Intent(MainActivity.this, activity_main_faq.class);
            startActivity(requestIntent);
        }
    });

    technician.setOnClickListener(new OnClickListener(){
        public void onClick(View arg0){
            Intent requestIntent = new Intent(MainActivity.this, submit_technician_request.class);
            startActivity(requestIntent);
        }
    });

    call.setOnClickListener(new OnClickListener(){ //dialog box
        public void onClick(View arg0){

        final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("Complete the call?");
        builder.setMessage("Click yes to connect your call. Otherwise, click no.");
        builder.setCancelable(true);
        builder.setIcon(R.drawable.ic_launcher);
      //saying yes completes the call. This way, we don't get accidently calls as often
        builder.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int id) {
                try {
                    Intent callIntent = new Intent(Intent.ACTION_CALL);
                    callIntent.setData(Uri.parse("tel:17656584294"));
                    startActivity(callIntent);
                } catch (ActivityNotFoundException activityException) {
                    Log.e("Calling a Phone Number", "Call failed", activityException);
                }
        }
          });
        builder.setNegativeButton("No",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int id) {
                dialog.cancel();
            }
        });

        // create alert dialog
        AlertDialog alertDialog = builder.create();

        // show it
        alertDialog.show();
    }
});



}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

splash.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:src="@raw/splash2" />

main_activity_main.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".basic_activity1"
    tools:ignore="MergeRootFrame"
    android:background="#000000" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/header"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#EAC117"
        android:textSize="35sp" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@raw/firstintentlogo"
        android:layout_weight="1" android:contentDescription="@string/headLogoName"/>

 </LinearLayout>

 <ScrollView
         android:id="@+id/scrollView1"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_marginTop="60dp" >

         <LinearLayout
             android:layout_width="match_parent"
             android:layout_height="fill_parent"
             android:orientation="vertical" >

             <Button
                 android:id="@+id/knowledgebase1"
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:text="@string/knowledgebase1"
                 android:textColor="#EAC117" />

             <Button
                 android:id="@+id/submitrequest1"
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:text="@string/submitrequest1"
                 android:textColor="#EAC117" />

            <Button
                 android:id="@+id/submitrequest2"
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:text="@string/submitTechnician"
                 android:textColor="#EAC117" />

             <Button
                 android:id="@+id/helpButton"
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:text="@string/helpButton"
                 android:textColor="#EAC117" />

             <Button
                 android:id="@+id/faqButton"
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:text="@string/faqButton"
                 android:textColor="#EAC117" />

              <Button
                 android:id="@+id/callButton"
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:text="@string/callButton"
                     android:textColor="#EAC117" />

            </LinearLayout>
         </ScrollView>
</FrameLayout>

1 个答案:

答案 0 :(得分:0)

立即调用

Initialize()

这是在调用MainActivity.this.setContentView(R.layout.activity_main)之前发生的。