hi sir I got this error when my app was launched. When I run my project unfortunately project was stop. This error is coming my log cat. When click on app unfortunately app was stop msg came.
我在activity_xml上放了一个文本视图。点击导航到另一个页面但这里不显示我的第一页。 mainactivity.java和activity main.xml存在问题 错误是java.lang.runtime异常:无法启动活动组件Info {com.example.b / com.example.b.MainActivity}:java。 Lang.NullPointerException
**mainactivity.java**
andheri = (TextView) findViewById(R.id.Andheri1);
andheri.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Context context = null;
// TODO Auto-generated method stub
Intent intent = new Intent(context,andheri.class);
startActivity(intent);
}
});
**activity_xml**
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/untitled1"
>
<TextView
android:id="@+id/Andher1"
android:layout_width="80dp"
android:layout_height="20dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#FFE4B5"
android:capitalize="characters"
android:gravity="center"
android:text="Andheri"
android:textColor="#000000"
android:textColorHint="#0000CD"
android:textSize="15dp"
android:textStyle="bold" />
</RelativeLayout>
</ScrollView>
**mainifest file.**
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.b.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.b.andheri"
android:label="@string/app_name" >
</activity>
</application>
答案 0 :(得分:2)
你应该改变这个
andheri = (TextView) findViewById(R.id.Andheri1);
到
andheri = (TextView) findViewById(R.id.Andher1);
您的TextView ID为id/Andher1
,并且您尝试将其设为@+id/Andheri1
。所以你得到NPE
。
答案 1 :(得分:0)
在您从主活动发送的意图中,您应该将上下文设置为此,而不是空
并更改textview的ID,您错过了'i'