Не могу понять, почему, но у меня нет ошибок на eclipse, (простая клавиатура app-button создает новую активность, отправляет в textView и imageView для нового действия), но как только я запустить мое приложение, и я прижимаюсь кнопкой, сбои приложений и я получаю эту ошибку на LogCatimage.setImageDrawable crashes app
Logcat
02-12 17:13:26.297: E/AndroidRuntime(398): Caused by: java.lang.NullPointerException
02-12 17:13:26.297: E/AndroidRuntime(398): at edu.colum.iam.SecondPage.onCreate(SecondPage.java:38)
Когда я беру эту строку из (и любой другой линии, которая будет воздействовать на него) приложение работает отлично, и он отправляет мой textView. Вот у меня есть .java, и я просто опубликую xml.
IntentsActivity.java
public class IntentsActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button)this.findViewById(R.id.button1);
btn.setOnClickListener(myListener);
}
// Create an anonymous implementation of OnClickListener
View.OnClickListener myListener = new View.OnClickListener()
{
public void onClick(View v) {
Intent myIntent = new Intent(IntentsActivity.this, SecondPage.class);
String text = ((TextView)findViewById(R.id.textView1)).getText().toString();
ImageView Selection = ((ImageView) findViewById(R.id.imageView1));
myIntent.putExtra("Text", text);
myIntent.putExtra("img", R.drawable.icon);
or an image)
startActivity(myIntent);
//endclass
SecondPage.java
public class SecondPage extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
//get extras
Intent intent = this.getIntent();
Bundle b = intent.getExtras();
String text = b.getString("Text");
//show text
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText(text);
//show image
ImageView image = (ImageView) findViewById(R.id.imageView1);
int resource = getIntent().getIntExtra("img", R.drawable.icon);
image.setImageDrawable(getResources().getDrawable(resource));
//^this is the line my logcat crashes as an error
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button android:text="@string/Button1" android:id="@+id/button1"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<TextView android:id="@+id/textView1" android:text="@string/FirstLayout"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content"></TextView>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon" />
</LinearLayout>
second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:layout_width="wrap_content" android:id="@+id/textView1"
android:text="@string/SecondLayout"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_height="wrap_content"></TextView>
<Button android:text="@string/Button2" android:id="@+id/button1"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
'imageView1' находится в основном макете вместо второго макета –