2015-05-14 66 views
0

無論何時單擊按鈕啓動下一個活動,我都會收到以下錯誤。該應用程序假設啓動用戶可以通過API創建帳戶並將數據存儲到SQL Server的活動。致命例外:主要 - 啓動另一活動時

下面是錯誤:

致命異常:主要了java.lang.RuntimeException:無法啓動 活動 ComponentInfo {com.durban.labapp_unisa/com.durban.labapp_unisa.CreateUserActivity}: 的java .lang.NullPointerException在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 在android.app.ActivityThread.access $ 600(ActivityThread。 java:141) at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1234) at 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) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)at dalvik.system.NativeStart.main(本地方法)由: java.lang.NullPointerException在 com.durban.labapp_unisa.CreateUserActivity.onCreate(CreateUserActivit y.java:44) 在android.app.Activity.performCreate(Activity.java:5104)在 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> 

請幫助我這個

+0

getApplicationContext()到class name.this我認爲這是問題 –

回答

0

嘗試更改下面的代碼

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

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

我認爲錯誤的是在那條線上...

相關問題