问题
I am having problem in starting a new activity on clicking a button, here's my code:
package test.project;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
public class TestActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View aboutButton = findViewById(R.id.about_content);
aboutButton.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.about_content:
Intent i = new Intent(this, testit.class);
startActivity(i);
break;
// More buttons go here (if any) ...
}
}
}
Can anyone please help me correct this error
Error Line
aboutButton.setOnClickListener(this);
Main.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click here" android:layout_gravity="center"
android:text="Click here" android:layout_gravity="center" android:layout_marginTop="30dip"/>
</LinearLayout>
XML file containing about_content is
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip" >
<TextView
android:id="@+id/about_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/about_text" />
</ScrollView>
about_content is already defined here
<TextView
android:id="@+id/about_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/about_text" />
回答1:
Well, you didn't post your logcat output, but since this is such a common beginner's mistake, I'm going to take a wild guess and say that you are probably getting a NullPointerException
.
Your call to findViewById
is probably returning null
, which means that the system could not find the view associated with the id given by R.id.about_content
. I would double check your XML layout for typos.
回答2:
Odds are you don't have anything with the id about_content
in main.xml, which will create a NullPointerException.
Also, if aboutButton
is supposed to be a traditional Button, then you should use this:
Button aboutButton = (Button) findViewById(R.id.about_content);
Addition
Since aboutButton
is a TextView, use this:
TextView aboutButton = (TextView) findViewById(R.id.about_content);
but this TextView must be in the layout passed to setContentView() or findViewById() will return null.
回答3:
That is because the "main.xml" which you have set your content view ... does not contain the about_content TextView, its in the other xml which you have posted ...
Note: You can access only those R.id's which are present in your setContentView(R.layout.yourlayout) xml ...
回答4:
you make setContentView(R.layout.main);
but main.xml
does not include View have id = R.id.about_content
. If you raplace by findViewById(R.id.button1);
It will work.
回答5:
This is the solution for
Button aboutButton = (Button)findViewById(R.id.about_content);
And dont forget to add testit Activity in Android Manifest
来源:https://stackoverflow.com/questions/10780305/force-close-error-on-setonclicklistener-example-from-hello-android-book-section