我收到错误(在我的机器人模拟器中)说#34;不幸的是,my_project_name已停止.."当我尝试运行该应用程序时。我很感激你的帮助。
以下是Logcat输出:
03-30 10:43:32.079: D/AndroidRuntime(1228): Shutting down VM
03-30 10:43:32.079: W/dalvikvm(1228): threadid=1: thread exiting with uncaught exception (group=0xb4a84ba8)
03-30 10:43:32.099: E/AndroidRuntime(1228): FATAL EXCEPTION: main
03-30 10:43:32.099: E/AndroidRuntime(1228): Process: com.example.testproject28_3_2014, PID: 1228
03-30 10:43:32.099: E/AndroidRuntime(1228): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testproject28_3_2014/com.example.testproject28_3_2014.MainActivity}: java.lang.NullPointerException
03-30 10:43:32.099: E/AndroidRuntime(1228): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-30 10:43:32.099: E/AndroidRuntime(1228): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-30 10:43:32.099: E/AndroidRuntime(1228): at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-30 10:43:32.099: E/AndroidRuntime(1228): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-30 10:43:32.099: E/AndroidRuntime(1228): at android.os.Handler.dispatchMessage(Handler.java:102)
03-30 10:43:32.099: E/AndroidRuntime(1228): at android.os.Looper.loop(Looper.java:136)
03-30 10:43:32.099: E/AndroidRuntime(1228): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-30 10:43:32.099: E/AndroidRuntime(1228): at java.lang.reflect.Method.invokeNative(Native Method)
03-30 10:43:32.099: E/AndroidRuntime(1228): at java.lang.reflect.Method.invoke(Method.java:515)
03-30 10:43:32.099: E/AndroidRuntime(1228): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-30 10:43:32.099: E/AndroidRuntime(1228): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-30 10:43:32.099: E/AndroidRuntime(1228): at dalvik.system.NativeStart.main(Native Method)
03-30 10:43:32.099: E/AndroidRuntime(1228): Caused by: java.lang.NullPointerException
03-30 10:43:32.099: E/AndroidRuntime(1228): at com.example.testproject28_3_2014.MainActivity.addSaveButtonListener(MainActivity.java:30)
03-30 10:43:32.099: E/AndroidRuntime(1228): at com.example.testproject28_3_2014.MainActivity.onCreate(MainActivity.java:21)
03-30 10:43:32.099: E/AndroidRuntime(1228): at android.app.Activity.performCreate(Activity.java:5231)
03-30 10:43:32.099: E/AndroidRuntime(1228): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-30 10:43:32.099: E/AndroidRuntime(1228): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-30 10:43:32.099: E/AndroidRuntime(1228): ... 11 more
以下是一些xml:
<LinearLayout 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:orientation="vertical"
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.example.testproject28_3_2014.MainActivity$PlaceholderFragment" >
<EditText
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="top"
android:hint="Enter some text here: " >
</EditText>
<Button
android:id="@+id/save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Save" >
</Button>
</LinearLayout>
这是我的mainactivity.java文件的前半部分(我在下半部分没有改变任何内容):
package com.example.testproject28_3_2014;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
public static final String DEBUGTAG = "DLD";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addSaveButtonListener();
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
private void addSaveButtonListener(){
Button saveBtn = (Button)findViewById(R.id.save);
saveBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.d(DEBUGTAG, "Save button clicked");
}
});
}
我在Stackoverflow上尝试过其他类似的查询,但这并没有帮助。我之前也有这个问题,有另一个应用程序,因此我想解决这个错误。
这是一些额外的代码,以帮助找到解决方案:
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;
}
}
}
答案 0 :(得分:0)
ID为save
的保存按钮不在活动布局中,而是在片段布局中。当您致电addSaveButtonListener()
并且findViewById()
返回null
时,它不在活动视图层次结构中。
将保存按钮设置移动到片段onCreateView()
,例如
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
Button saveBtn = (Button)rootView.findViewById(R.id.save);
saveBtn.setOnClickListener(...);
return rootView;
答案 1 :(得分:0)
使用此
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
public static final String DEBUGTAG = "DLD";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button saveBtn = (Button)findViewById(R.id.save);
setContentView(R.layout.activity_main);
addSaveButtonListener();
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
private void addSaveButtonListener(){
saveBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.d(DEBUGTAG, "Save button clicked");
}
});
}