Android应用程序已停止工作-eclipse [英] Android application has stopped working -eclipse
问题描述
我一直在试图建立我的第一个Android应用程序。每次我打开模拟器的应用,我刚刚得到一个消息,说测试已停止工作。它必须是简单的东西。希望你能帮助我。
I have been tried to build my first Android Application. Every time I open the application in the emulator, I just get a message that says test has stopped working. It must be something simple. Hope you can help me.
15 04-06:43:47.806:W / dalvikvm(4275):主题ID = 1:螺纹退出
未捕获的异常(组= 0xa614d908)
04-06 15:43:47.806: W/dalvikvm(4275): threadid=1: thread exiting with uncaught exception (group=0xa614d908)
15 04-06:43:47.826:E / AndroidRuntime(4275):致命异常:主要
04-06 15:43:47.826: E/AndroidRuntime(4275): FATAL EXCEPTION: main
15 04-06:43:47.826:E / AndroidRuntime(4275):
了java.lang.RuntimeException:无法启动活动
ComponentInfo {com.test / com.test.MainActivity}:
显示java.lang.NullPointerException
04-06 15:43:47.826: E/AndroidRuntime(4275): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test/com.test.MainActivity}: java.lang.NullPointerException
15 04-06:43:47.826:E / AndroidRuntime(4275):在
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-06 15:43:47.826: E/AndroidRuntime(4275): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
15 04-06:43:47.826:E / AndroidRuntime(4275):在
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-06 15:43:47.826: E/AndroidRuntime(4275): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
15 04-06:43:47.826:E / AndroidRuntime(4275):在
android.app.ActivityThread.access $ 600(ActivityThread.java:141)
04-06 15:43:47.826: E/AndroidRuntime(4275): at android.app.ActivityThread.access$600(ActivityThread.java:141)
15 04-06:43:47.826:E / AndroidRuntime(4275):在
android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1234)
04-06 15:43:47.826: E/AndroidRuntime(4275): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
15 04-06:43:47.826:E / AndroidRuntime(4275):在
android.os.Handler.dispatchMessage(Handler.java:99)
04-06 15:43:47.826: E/AndroidRuntime(4275): at android.os.Handler.dispatchMessage(Handler.java:99)
15 04-06:43:47.826:E / AndroidRuntime(4275):在
android.os.Looper.loop(Looper.java:137)
04-06 15:43:47.826: E/AndroidRuntime(4275): at android.os.Looper.loop(Looper.java:137)
15 04-06:43:47.826:E / AndroidRuntime(4275):在
android.app.ActivityThread.main(ActivityThread.java:5041)
04-06 15:43:47.826: E/AndroidRuntime(4275): at android.app.ActivityThread.main(ActivityThread.java:5041)
15 04-06:43:47.826:E / AndroidRuntime(4275):在
java.lang.reflect.Method.invokeNative(本机方法)
04-06 15:43:47.826: E/AndroidRuntime(4275): at java.lang.reflect.Method.invokeNative(Native Method)
15 04-06:43:47.826:E / AndroidRuntime(4275):在
java.lang.reflect.Method.invoke(Method.java:511)
04-06 15:43:47.826: E/AndroidRuntime(4275): at java.lang.reflect.Method.invoke(Method.java:511)
15 04-06:43:47.826:E / AndroidRuntime(4275):在
com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)
04-06 15:43:47.826: E/AndroidRuntime(4275): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
15 04-06:43:47.826:E / AndroidRuntime(4275):在
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-06 15:43:47.826: E/AndroidRuntime(4275): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
15 04-06:43:47.826:E / AndroidRuntime(4275):在
dalvik.system.NativeStart.main(本机方法)
04-06 15:43:47.826: E/AndroidRuntime(4275): at dalvik.system.NativeStart.main(Native Method)
15 04-06:43:47.826:E / AndroidRuntime(4275):产生的原因:
显示java.lang.NullPointerException
04-06 15:43:47.826: E/AndroidRuntime(4275): Caused by: java.lang.NullPointerException
15 04-06:43:47.826:E / AndroidRuntime(4275):在
com.test.MainActivity.onCreate(MainActivity.java:30)
04-06 15:43:47.826: E/AndroidRuntime(4275): at com.test.MainActivity.onCreate(MainActivity.java:30)
15 04-06:43:47.826:E / AndroidRuntime(4275):在
android.app.Activity.performCreate(Activity.java:5104)
04-06 15:43:47.826: E/AndroidRuntime(4275): at android.app.Activity.performCreate(Activity.java:5104)
15 04-06:43:47.826:E / AndroidRuntime(4275):在
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-06 15:43:47.826: E/AndroidRuntime(4275): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
15 04-06:43:47.826:E / AndroidRuntime(4275):在
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-06 15:43:47.826: E/AndroidRuntime(4275): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
15 04-06:43:47.826:E / AndroidRuntime(4275):11 ...更多
04-06 15:43:47.826: E/AndroidRuntime(4275): ... 11 more
以上是日志文件
public class Main extends ActionBarActivity {
int counter ;
Button add, sub ;
TextView display;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = 0;
add = (Button) findViewById (R.id.bAdd);
sub = (Button) findViewById (R.id.bsub);
display = (TextView) findViewById(R.id.tvDisplay);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter++;
display.setText("Your total is " + counter);
}
});
sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter --;
display.setText("Your total is " + counter);
}
});
if (savedInstanceState == null) {
getSupportFragmentManager().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;
}
}
}
以上是主类
<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: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="com.test.MainActivity$PlaceholderFragment" >
<TextView
android:id="@+id/tvDisplay"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignLeft="@+id/bsub"
android:layout_alignParentBottom="true"
android:layout_below="@+id/bsub"
android:gravity="center"
android:text="Your total is 0"
android:textSize="45dp" />
<Button
android:id="@+id/badd"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp"
android:layout_marginTop="78dp"
android:gravity="center"
android:text="Add one"
android:textSize="20dp" />
<Button
android:id="@+id/bsub"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/badd"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="Subtract one"
android:textSize="20dp" />
</RelativeLayout>
以上是fragment_main.xml
Above is the fragment_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.Hello.firstapp.Main"
tools:ignore="MergeRootFrame" />
上面是activity_main.xml中
ABove is the activity_main.xml
推荐答案
/ code>那些按钮
和的TextView
是在乌拉圭回合 fragment_main.xml
这就是为什么你长了 NPE
那些按钮
和的TextView
不是 activity_main.xml中
的一部分。
Instead of onCreate()
move all ur initialization into onCreateView()
as those buttons
and textview
is in ur fragment_main.xml
thats why u got NPE
as those buttons
and textview
is not the part of activity_main.xml
.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
counter = 0;
add = (Button) rootView.findViewById (R.id.bAdd);
sub = (Button) rootView.findViewById (R.id.bsub);
display = (TextView) rootView.findViewById(R.id.tvDisplay);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter++;
display.setText("Your total is " + counter);
}
});
sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter --;
display.setText("Your total is " + counter);
}
});
return rootView;
}
这篇关于Android应用程序已停止工作-eclipse的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!