1

我是Android新手,只是想弄清楚如何使用onActivityResult方法。我想单击mainActivity上的按钮,转到另一个actvivity,输入名称并返回mainActivity。但是我在运行应用程序时遇到了麻烦。我总是得到这个错误:使用意图不能调用其他活动

java.lang.NullPointerException: Attempt to invoke virtual method...

09-20 22:14:32.212 30617-30617/com.genaepic.p029_simpleactivityresult E/AndroidRuntime: 

FATAL EXCEPTION: main Process: com.genaepic.p029_simpleactivityresult, PID: 30617 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.genaepic.p029_simpleactivityresult/com.genaepic.p029_simpleactivityresult.NameActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at com.genaepic.p029_simpleactivityresult.NameActivity.onCreate(NameActivity.java:22) at android.app.Activity.performCreate(Activity.java:6679) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)  at android.app.ActivityThread.-wrap12(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:154)  at android.app.ActivityThread.main(ActivityThread.java:6119)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

我的主要活动:

public class MainActivity extends AppCompatActivity implements View.OnClickListener { 

    TextView textView; 
    Button button; 


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

     textView = (TextView) findViewById(R.id.tv_main); 
     button = (Button) findViewById(R.id.btn_iputName); 
     button.setOnClickListener(this); 
    } 

    @Override 
    public void onClick(View v) { 
     Intent intent = new Intent(this, NameActivity.class); 
     startActivityForResult(intent, 1); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 

     if(data == null) { 
      return; 
     } 
     String name = data.getStringExtra("name"); 
     textView.setText("Your name is " + name); 
    } 
} 

我的第二个活动:为mainActivity

public class NameActivity extends AppCompatActivity implements View.OnClickListener { 

EditText editText; 
Button button; 

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

    editText = (EditText) findViewById(R.id.et_name); 
    button = (Button) findViewById(R.id.btn_iputName); 
    button.setOnClickListener(this); 
} 

@Override 
public void onClick(View v) { 
    Intent intent = new Intent(); 
    intent.putExtra("name", editText.getText().toString()); 
    setResult(RESULT_OK, intent); 
    finish(); 
} 

}

我的XML代码:

<Button 
    android:id="@+id/btn_iputName" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="input your name" 
    tools:layout_constraintTop_creator="1" 
    android:layout_marginStart="34dp" 
    android:layout_marginTop="92dp" 
    tools:layout_constraintLeft_creator="1" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintTop_toTopOf="parent" 
    android:layout_marginLeft="34dp" /> 

<TextView 
    android:id="@+id/tv_main" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Your name is" 
    tools:layout_constraintTop_creator="1" 
    android:layout_marginStart="34dp" 
    android:layout_marginTop="33dp" 
    tools:layout_constraintLeft_creator="1" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintTop_toTopOf="parent" 
    android:layout_marginLeft="34dp" /> 

为NameActivity

<Button 
     android:id="@+id/btn_ok" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="116dp" 
     android:text="ok" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     tools:layout_constraintLeft_creator="1" 
     tools:layout_constraintRight_creator="1" 
     tools:layout_constraintTop_creator="1" /> 

    <EditText 
     android:id="@+id/et_name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginEnd="64dp" 
     android:layout_marginRight="84dp" 
     android:layout_marginTop="31dp" 
     android:ems="10" 
     android:inputType="textPersonName" 
     android:text="Name" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     tools:layout_constraintRight_creator="1" 
     tools:layout_constraintTop_creator="1" /> 
</android.support.constraint.ConstraintLayout> 
+0

您可以使用其中之一旁边的复选标记接受答案 –

回答

3

你有

setContentView(R.layout.activity_name) 

我的XML代码,从而改变这个

button = (Button) findViewById(R.id.btn_iputName) 

button = (Button) findViewById(R.id.btn_ok); 

findViewById将在当前视图层次结构中查找带有id的视图。所以如果你有setContentView(R.layout.activity_name)你应该在你的activity_name.xml中查找id。

+0

您是对的。非常感谢) –

2

在你的NameActivity中没有按钮=(Button)findViewById(R.id.btn_iputName);相反,它存在于MainActivity中,并且您正在NameActivity中查找,因为在启动Name活动时它会给出NullPointerException。

+0

谢谢,它适用于我 –

1

用于按钮

在你的第二个活动,即错误ID,NameActivity.java 需要更换

button = (Button) findViewById(R.id.btn_iputName); 

button = (Button) findViewById(R.id.btn_ok); 

These changes will resolve the NullPointerException and your app will work properly.

+0

谢谢你,帮助 –

4

你在做NameActivity setContentView(R.layout.activity_name),然后button = (Button) findViewById(R.id.btn_iputName)这是存在的在另一项活动中。

只要改变

button = (Button) findViewById(R.id.btn_iputName); 

button = (Button) findViewById(R.id.btn_ok); 

它会为你工作。

+0

感谢您的反馈意见。这很有帮助 –

相关问题