现在我已经搜索了几个小时来查找代码中的失败。 我的代码中有NPE。
使用ImageButton和setOnClickListener。
这里的堆栈跟踪:
04-01 23:37:43.912: W/asset(6416): Copying FileAsset 0x77cecd20 (zip:/data/app/net.test.apk:/resources.arsc) to buffer size 7000 to make it aligned.
04-01 23:37:43.962: W/dalvikvm(6416): threadid=1: thread exiting with uncaught exception (group=0x416736f8)
04-01 23:37:43.972: E/AndroidRuntime(6416): FATAL EXCEPTION: main
04-01 23:37:43.972: E/AndroidRuntime(6416): Process: net.test, PID: 6416
04-01 23:37:43.972: E/AndroidRuntime(6416): java.lang.RuntimeException: Unable to start activity ComponentInfo{net.test/net.test.MainActivity}: java.lang.NullPointerException
04-01 23:37:43.972: E/AndroidRuntime(6416): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
04-01 23:37:43.972: E/AndroidRuntime(6416): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
04-01 23:37:43.972: E/AndroidRuntime(6416): at android.app.ActivityThread.access$800(ActivityThread.java)
04-01 23:37:43.972: E/AndroidRuntime(6416): at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
04-01 23:37:43.972: E/AndroidRuntime(6416): at android.os.Handler.dispatchMessage(Handler.java)
04-01 23:37:43.972: E/AndroidRuntime(6416): at android.os.Looper.loop(Looper.java)
04-01 23:37:43.972: E/AndroidRuntime(6416): at android.app.ActivityThread.main(ActivityThread.java)
04-01 23:37:43.972: E/AndroidRuntime(6416): at java.lang.reflect.Method.invokeNative(Native Method)
04-01 23:37:43.972: E/AndroidRuntime(6416): at java.lang.reflect.Method.invoke(Method.java)
04-01 23:37:43.972: E/AndroidRuntime(6416): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
04-01 23:37:43.972: E/AndroidRuntime(6416): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
04-01 23:37:43.972: E/AndroidRuntime(6416): at dalvik.system.NativeStart.main(Native Method)
04-01 23:37:43.972: E/AndroidRuntime(6416): Caused by: java.lang.NullPointerException
04-01 23:37:43.972: E/AndroidRuntime(6416): at net.test.MainActivity.onCreate(MainActivity.java:24)
04-01 23:37:43.972: E/AndroidRuntime(6416): at android.app.Activity.performCreate(Activity.java)
04-01 23:37:43.972: E/AndroidRuntime(6416): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)
这里是.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/main_back"
android:gravity="center_horizontal"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="net.ringmybuddy.MainActivity$PlaceholderFragment" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="140dp"
android:contentDescription="@string/button_step1"
android:src="@drawable/start_image_step1" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/imageView1"
android:layout_below="@+id/imageView1"
android:contentDescription="@string/button_step2"
android:src="@drawable/start_image_step2" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/imageView2"
android:layout_below="@+id/imageView2"
android:contentDescription="@string/button_step3"
android:src="@drawable/start_image_step3" />
<ImageView
android:id="@+id/imageView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/imageView3"
android:layout_below="@+id/imageView3"
android:layout_marginTop="40dp"
android:contentDescription="@string/start_rmb_schriftzug"
android:src="@drawable/start_rmb_schriftzug" />
<ImageButton
android:id="@+id/einrichten"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView4"
android:layout_centerHorizontal="true"
android:layout_marginTop="0dp"
android:contentDescription="@string/button_einrichten"
android:src="@drawable/start_button_einrichten" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="55dp"
android:gravity="center_horizontal"
android:text="@string/startroutine_einleitung"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#ffffff"
android:textSize="15sp"
android:textStyle="normal"
android:typeface="normal" />
<ImageView
android:id="@+id/imageView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/newtest"
android:layout_marginBottom="17dp"
android:layout_marginLeft="11dp"
android:layout_toRightOf="@+id/newtest"
android:contentDescription="@string/pfeil_2arrows"
android:src="@drawable/start_image_arrows" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="0dp"
android:text="@string/copyright"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#bcb081"
android:textSize="12sp" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="-5dp"
android:text="@string/startroutine_ueberschrift"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#ffffff"
android:textSize="18sp"
android:textStyle="bold"
android:typeface="normal" />
</RelativeLayout>
MainActivity:
package net.test;
import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
public class MainActivity extends Activity implements View.OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton einrichten = (ImageButton)findViewById(R.id.einrichten);
einrichten.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Intent in = new Intent
(
MainActivity.this,
EinstellungenMainActivityOn.class
);
startActivity(in);
}
});
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
是的,我知道它在MainActivity的第24行,但我不知道如何解决这个问题。 我已经尝试了一切,没有任何东西对我有用。 我希望你们能帮助我。 非常感谢提前!
编辑:对不起,我忘了添加MainActivity。
答案 0 :(得分:1)
您在einrichten
中没有activity_main
个ID,您为我们发布的xml
代码是fragment_main
的片段,因此您必须拥有一个ImageButton在einrichten
中使用activity_main
id或处理单击片段类。
我为你编写改变片段的代码,
所以你的片段代码必须是:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
ImageButton einrichten = (ImageButton)rootView.findViewById(R.id.einrichten);
einrichten.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Intent in = new Intent
(
getActivity(),
EinstellungenMainActivityOn.class
);
startActivity(in);
}
});
return rootView;
}
并在MainActivity中:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}