致命异性:主要 - 启动另一项活动时

时间:2015-05-14 12:19:51

标签: android eclipse nullpointerexception

每当我点击按钮启动下一个活动时,我都会收到以下错误。该应用程序用于启动用户可以创建帐户并通过API将数据存储到SQL Server的活动。

以下是错误:

  

FATAL EXCEPTION:main java.lang.RuntimeException:无法启动   活动   ComponentInfo {com.durban.labapp_unisa / com.durban.labapp_unisa.CreateUserActivity}:   java.lang.NullPointerException at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)     在   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)     在android.app.ActivityThread.access $ 600(ActivityThread.java:141)     在   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1234)     在android.os.Handler.dispatchMessage(Handler.java:99)at   android.os.Looper.loop(Looper.java:137)at   android.app.ActivityThread.main(ActivityThread.java:5041)at   java.lang.reflect.Method.invokeNative(Native Method)at   java.lang.reflect.Method.invoke(Method.java:511)at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)     在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)at   dalvik.system.NativeStart.main(Native方法)引起:   java.lang.NullPointerException at   com.durban.labapp_unisa.CreateUserActivity.onCreate(CreateUserActivity.java:44)     在android.app.Activity.performCreate(Activity.java:5104)at   android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)     在   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)     ......还有11个

我的主要活动是:

public class MainActivity extends Activity implements OnClickListener {

Button btnLogin, btnRegister, btnDisplay;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

  //Initialize the layout components
    btnLogin = (Button) findViewById(R.id.btnLogin);
    btnRegister = (Button) findViewById(R.id.btnRegister);

  //set onClick listeners for buttons
    btnLogin.setOnClickListener(this);
    btnRegister.setOnClickListener(this);
}

@Override  
public void onClick(View v) {

    switch(v.getId()){

    case R.id.btnRegister:
        Intent cr = new Intent(getApplicationContext(), CreateUserActivity.class);
        startActivity(cr);
        break;
    }
}

@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;
}

}

另一项活动是:

/*
* This activity used to create users. 
* When user is created it will redirect to LoginActivity.
*/
public class CreateUserActivity extends Activity {

EditText StudentNr, Surname, Fullname, Password,Cell, Qual;
Button Save, backButton;

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_create_user);

// Show the Up button in the action bar.
  setupActionBar();

//setup button listener
  StudentNr = (EditText) findViewById(R.id.etStudentNr);
  Surname = (EditText) findViewById(R.id.etSurname);
  Fullname = (EditText) findViewById(R.id.etFullname);
  Password = (EditText) findViewById(R.id.etPass1);
  Cell = (EditText) findViewById(R.id.etCell);
  Qual = (EditText) findViewById(R.id.etQua);
  Save = (Button) findViewById(R.id.btnRegister); 
  backButton = (Button) findViewById(R.id.btnBack1);

  Save.setOnClickListener(new View.OnClickListener() {        
      @Override
      public void onClick(View v) {
            // TODO Auto-generated method stub

            String studentNr, surname, fullname, password, cell, qual;

            studentNr = StudentNr.getText().toString();
            surname = Surname.getText().toString();
            fullname = Fullname.getText().toString();
            password = Password.getText().toString();
            cell = Cell.getText().toString();
            qual = Qual.getText().toString();

            UserDetailsTable Records = new UserDetailsTable(studentNr,surname, fullname, password, cell, qual);

            new AsyncCreateUser().execute(Records);
      }
  });   

  backButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
          finish();
    }
  });

}

protected class AsyncCreateUser extends AsyncTask<UserDetailsTable, Void, Void>{

    @Override
    protected Void doInBackground(UserDetailsTable... params){

        RestAPI api = new RestAPI();
        try{

            api.CreateNewAccount(params[0].getStudentNr(), params[1].getSurname(), params[2].getFullname(), params[3].getPassword(), params[4].getCellNr(), params[5].getQual());

        }catch(Exception e){
            Log.d("AsyncCreateUser", e.getMessage());
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result){

        //launch the login page after account successfully created
        Intent i = new Intent(getApplicationContext(), MainActivity.class);
        startActivity(i);
    }
}

/**
 * Set up the {@link android.app.ActionBar}, if the API is available.
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() {
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            getActionBar().setDisplayHomeAsUpEnabled(true);
      }
}

@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;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

      switch (item.getItemId()) {
      case android.R.id.home:
            // This ID represents the Home or Up button. In the case of this
            // activity, the Up button is shown. Use NavUtils to allow users
            // to navigate up one level in the application structure. For
            // more details, see the Navigation pattern on Android Design:
            //
            // http://developer.android.com/design/patterns/navigation.html#up-vs-back
            //
            NavUtils.navigateUpFromSameTask(this);
            return true;
      }
      return super.onOptionsItemSelected(item);
}

清单看起来像:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.durban.labapp_unisa.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name="com.durban.labapp_unisa.CreateUserActivity"
        android:label="@string/app_name"
        android:launchMode="singleTop"
        android:stateNotNeeded="true">
        <intent-filter >
            <action android:name="android.intent.action.CREATEUSERACTIVITY"/>
            <action android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
     </activity> 

    <activity
        android:name="com.durban.labapp_unisa.JSONParser"
        android:label="@string/app_name">
        <intent-filter >
            <action android:name="android.intent.action.JSONPARSER"/>
            <action android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
     </activity>

    <activity
        android:name="com.durban.labapp_unisa.RestAPI"
        android:label="@string/app_name">
        <intent-filter >
            <action android:name="android.intent.action.RESTAPI"/>
            <action android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
     </activity>

    <activity
        android:name="com.durban.labapp_unisa.UserDetailsTable"
        android:label="@string/app_name">
        <intent-filter >
            <action android:name="android.intent.action.USERDETAILSTABLE"/>
            <action android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
     </activity>
</application>

请帮我解决这个问题

1 个答案:

答案 0 :(得分:0)

尝试更改以下代码

    Intent cr = new Intent(getApplicationContext(), CreateUserActivity.class);
    startActivity(cr);

    Intent cr = new Intent(MainActivity.this, CreateUserActivity.class);
    startActivity(cr);

我认为错误就在那条线上......