我是新的 Android 编码器,我正在使用 startActivityForResult 函数。当我单击启动此方法的按钮时,我无法进入另一个活动,因为它崩溃了:
所以有 MainActivity.java 类。在 public void setRegisterOnClick(View v) 中,我想使用 startActivityForResult 两次,将其他意图发送到同一活动。Intents 包含传递给 Users.class 并用于声明 giveResponse 的 int 编号。但是每次我单击按钮启动 startActivityForResult 我的应用程序崩溃。
package com.example.login;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final int USERS_REQUEST_CODE=1;
private static String sActualLogin = "admin";
private static String sActualPassword = "qwerty";
private static String sContainer = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@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;
}
public void setLoginOnClick(View v)
{
EditText etLogin = (EditText) findViewById(R.id.tfLogin);
EditText etPassword = (EditText) findViewById(R.id.tfPassword);
String sLogin = etLogin.getText().toString();
String sPassword = etPassword.getText().toString();
if (sLogin.equals(sActualLogin) && sPassword.equals(sActualPassword))
{
Intent intent = new Intent (this, Inside.class);
startActivity(intent);
}
else
{
Toast.makeText(this, "Invalid username or password.", Toast.LENGTH_LONG).show();
}
}
public void setRegisterOnClick(View v)
{
Intent intentLOGIN = new Intent(this, Users.class);
intentLOGIN.putExtra("Type",1);
startActivityForResult(intentLOGIN, USERS_REQUEST_CODE);
sActualLogin=sContainer;
Intent intentPASSWORD= new Intent(this, Users.class);
intentPASSWORD.putExtra("Type",2);
startActivityForResult(intentPASSWORD, USERS_REQUEST_CODE);
sActualPassword=sContainer;
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
sContainer = data.getStringExtra(Users.RESPONSE);
}
}
用户类
package com.example.login;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class Users extends Activity {
public static final String RESPONSE = "Response";
EditText etNewLogin = (EditText) findViewById(R.id.etChangeLogin);
EditText etNewPassword = (EditText) findViewById(R.id.etChangePassword);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.users);
}
public void giveResponse(View v)
{
Intent intent = getIntent();
int iOperation = intent.getIntExtra("Type", 0);
if (iOperation == 1){
String sNewLogin = etNewLogin.getText().toString();
Intent resultIntent1 = new Intent();
resultIntent1.putExtra(RESPONSE, sNewLogin);
setResult(RESULT_OK, resultIntent1);
finish();
}
if (iOperation == 2){
String sNewPassword = etNewPassword.getText().toString();
Intent resultIntent2 = new Intent();
resultIntent2.putExtra(RESPONSE, sNewPassword);
setResult(RESULT_OK, resultIntent2);
finish();
}
}
}
清单片段:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.login.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.example.login.Users"></activity>
<activity android:name="com.example.login.Inside"></activity>
</application>
错误:
05-12 20:22:10.350: W/dalvikvm(6349): threadid=1: thread exiting with uncaught exception (group=0x40a191f8)
05-12 20:22:10.360: E/AndroidRuntime(6349): FATAL EXCEPTION: main
05-12 20:22:10.360: E/AndroidRuntime(6349): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.login/com.example.login.Users}: java.lang.NullPointerException
05-12 20:22:10.360: E/AndroidRuntime(6349): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880)
05-12 20:22:10.360: E/AndroidRuntime(6349): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-12 20:22:10.360: E/AndroidRuntime(6349): at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-12 20:22:10.360: E/AndroidRuntime(6349): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-12 20:22:10.360: E/AndroidRuntime(6349): at android.os.Handler.dispatchMessage(Handler.java:99)
05-12 20:22:10.360: E/AndroidRuntime(6349): at android.os.Looper.loop(Looper.java:137)
05-12 20:22:10.360: E/AndroidRuntime(6349): at android.app.ActivityThread.main(ActivityThread.java:4429)
05-12 20:22:10.360: E/AndroidRuntime(6349): at java.lang.reflect.Method.invokeNative(Native Method)
05-12 20:22:10.360: E/AndroidRuntime(6349): at java.lang.reflect.Method.invoke(Method.java:511)
05-12 20:22:10.360: E/AndroidRuntime(6349): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
05-12 20:22:10.360: E/AndroidRuntime(6349): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
05-12 20:22:10.360: E/AndroidRuntime(6349): at dalvik.system.NativeStart.main(Native Method)
05-12 20:22:10.360: E/AndroidRuntime(6349): Caused by: java.lang.NullPointerException
05-12 20:22:10.360: E/AndroidRuntime(6349): at android.app.Activity.findViewById(Activity.java:1794)
05-12 20:22:10.360: E/AndroidRuntime(6349): at com.example.login.Users.<init>(Users.java:13)
05-12 20:22:10.360: E/AndroidRuntime(6349): at java.lang.Class.newInstanceImpl(Native Method)
05-12 20:22:10.360: E/AndroidRuntime(6349): at java.lang.Class.newInstance(Class.java:1319)
05-12 20:22:10.360: E/AndroidRuntime(6349): at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
05-12 20:22:10.360: E/AndroidRuntime(6349): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1871)
05-12 20:22:10.360: E/AndroidRuntime(6349): ... 11 more
好吧,我为这种微不足道的问题写了很多(我猜是:P),但是由于我没有经验,因此需要花费数小时来解决它,但我仍然不明白。请指导/帮助我。