首先在MainActivity中,当按下按钮时,我创建一个新意图并调用它。然后,在第二个意图中,我创建一个新意图,并在按下另一个按钮时调用它。我可以从MainActivity调用intent,但无法从第二个活动中调用新创建的intent。
另外,我在AndroidManifest.xml中创建了所有意图的条目。
错误如下:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.asad.reflect/com.example.asad.reflect.Superclass}: java.lang.NullPointerException
MainActivity.java
public void onClick(View v)
{
if(v.getId()==R.id.b1)
{
Intent i=new Intent(this,Callinterface.class);
startActivity(i);
}
else
{
Intent i=new Intent(this,Callclass.class);
startActivity(i);
}
}
Callclass.java
public void onClick(View v) {
et=(EditText)findViewById(R.id.textclass);
className=et.getText().toString();
Intent i=new Intent(this,Superclass.class);
startActivity(i);
}
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.asad.reflect">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Callclass"
android:label="Callclass"
android:theme="@style/AppTheme.NoActionBar">
</activity>
<activity
android:name=".Callinterface"
android:label="Callinterface"
android:theme="@style/AppTheme.NoActionBar"></activity>
<activity
android:name=".Superclass"
android:label="Superclass"
android:theme="@style/AppTheme.NoActionBar"></activity>
<!-- ATTENTION: This was auto-generated to add Google Play services to your project for
App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information. -->
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
</manifest>
答案 0 :(得分:0)
尝试将代码更改为
else if(v.getId() == R.id.button2){
//Run your button 2 Intent
}else if(v.getId() == R.id.button3){
//Run Button 3 Intent
}