我尝试将字符串从活动发送到选项卡中的片段,但我不断获得空堆栈指针异常。 这是主活动中将字符串发送到片段
的代码部分//this portion of code sends the user_name to the note fragment
Bundle bundle = new Bundle();
bundle.putString("name", name);
NoteFragment NF = new NoteFragment();
NF.setArguments(bundle);
这段代码接收片段类中的字符串。
//fragment class
public class NoteFragment extends Fragment{
View v;
String note, name, timestamp;
EditText NOTE;
noteOperations NO;
Context ctx;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
v = inflater.inflate(R.layout.notelayout, container, false);
ctx = getActivity().getApplicationContext();
NOTE = (EditText) v.findViewById(R.id.note);
NO = new noteOperations(ctx);
name = getArguments().getString("name");
ImageButton save = (ImageButton) v.findViewById(R.id.saveButton);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
note = NOTE.getText().toString();
if (!note.equals(""))
{
timestamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance ().getTime());
NO.putInformation(NO, note, timestamp, name);
NOTE.setText("");
}
}
});
return v;
}
}
这是logcat
10-04 06:52:48.848: D/Databsse operations(876): Database created
10-04 06:52:50.151: D/Databsse operations(876): One row inserted
10-04 06:52:51.207: D/gralloc_goldfish(876): Emulator without GPU emulation detected.
10-04 06:53:35.698: I/Choreographer(876): Skipped 49 frames! The application may be doing too much work on its main thread.
10-04 06:53:36.648: D/dalvikvm(876): GC_CONCURRENT freed 72K, 7% free 2716K/2908K, paused 15ms+175ms, total 1283ms
10-04 06:53:38.288: I/Choreographer(876): Skipped 130 frames! The application may be doing too much work on its main thread.
10-04 06:53:44.859: I/Choreographer(876): Skipped 34 frames! The application may be doing too much work on its main thread.
10-04 06:53:49.748: D/Databsse operations(876): Database created
10-04 06:53:49.918: D/Databsse operations(876): Table combed
10-04 06:53:50.291: I/Choreographer(876): Skipped 147 frames! The application may be doing too much work on its main thread.
10-04 06:53:50.648: I/Choreographer(876): Skipped 41 frames! The application may be doing too much work on its main thread.
10-04 06:53:53.247: D/dalvikvm(876): GC_FOR_ALLOC freed 208K, 11% free 2892K/3224K, paused 501ms, total 555ms
10-04 06:53:53.378: I/dalvikvm-heap(876): Grow heap (frag case) to 3.545MB for 635812-byte allocation
10-04 06:53:53.858: D/dalvikvm(876): GC_FOR_ALLOC freed 2K, 9% free 3511K/3848K, paused 481ms, total 482ms
10-04 06:53:54.938: D/AndroidRuntime(876): Shutting down VM
10-04 06:53:54.958: W/dalvikvm(876): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
10-04 06:53:55.128: D/dalvikvm(876): GC_CONCURRENT freed 33K, 9% free 3518K/3848K, paused 89ms+281ms, total 1265ms
10-04 06:53:55.258: E/AndroidRuntime(876): FATAL EXCEPTION: main
10-04 06:53:55.258: E/AndroidRuntime(876): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.databank/com.example.databank.Dashboard}: java.lang.NullPointerException
10-04 06:53:55.258: E/AndroidRuntime(876): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
10-04 06:53:55.258: E/AndroidRuntime(876): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
10-04 06:53:55.258: E/AndroidRuntime(876): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-04 06:53:55.258: E/AndroidRuntime(876): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
10-04 06:53:55.258: E/AndroidRuntime(876): at android.os.Handler.dispatchMessage(Handler.java:99)
10-04 06:53:55.258: E/AndroidRuntime(876): at android.os.Looper.loop(Looper.java:137)
10-04 06:53:55.258: E/AndroidRuntime(876): at android.app.ActivityThread.main(ActivityThread.java:5041)
10-04 06:53:55.258: E/AndroidRuntime(876): at java.lang.reflect.Method.invokeNative(Native Method)
10-04 06:53:55.258: E/AndroidRuntime(876): at java.lang.reflect.Method.invoke(Method.java:511)
10-04 06:53:55.258: E/AndroidRuntime(876): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-04 06:53:55.258: E/AndroidRuntime(876): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-04 06:53:55.258: E/AndroidRuntime(876): at dalvik.system.NativeStart.main(Native Method)
10-04 06:53:55.258: E/AndroidRuntime(876): Caused by: java.lang.NullPointerException
10-04 06:53:55.258: E/AndroidRuntime(876): at com.example.databank.NoteFragment.<init>(NoteFragment.java:17)
10-04 06:53:55.258: E/AndroidRuntime(876): at com.example.databank.Dashboard.onCreate(Dashboard.java:38)
10-04 06:53:55.258: E/AndroidRuntime(876): at android.app.Activity.performCreate(Activity.java:5104)
10-04 06:53:55.258: E/AndroidRuntime(876): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
10-04 06:53:55.258: E/AndroidRuntime(876): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
10-04 06:53:55.258: E/AndroidRuntime(876): ... 11 more
10-04 06:54:01.578: I/Process(876): Sending signal. PID: 876 SIG: 9
10-04 06:54:04.928: D/Databsse operations(900): Database created
10-04 06:54:06.328: D/Databsse operations(900): One row inserted
10-04 06:54:07.408: I/Choreographer(900): Skipped 52 frames! The application may be doing too much work on its main thread.
10-04 06:54:07.498: D/gralloc_goldfish(900): Emulator without GPU emulation detected.
答案 0 :(得分:0)
堆栈跟踪清楚地表明您的NullPointerException位于NoteFragment.java中的第17行
您不包含行号,但您示例的第16行和第17行是:
ImageButton save = (ImageButton) v.findViewById(R.id.saveButton);
save.setOnClickListener(new View.OnClickListener() {
所以我猜测你的变量“保存”#39;为空,因为v.findViewById(R.id.saveButton)
没有找到任何内容。
哦,它是一个NullPointerException,而不是&#34; null stackpointer exception&#34;这与堆栈无关。
答案 1 :(得分:0)
在fragment类中,我初始化了一个新的bundle。这是修改
Bundle bundle = new Bundle();
String name = bundle.getString("name");
这纠正了空指针异常
答案 2 :(得分:0)
如果初始化Bundle并且它纠正了空指针异常,那意味着getArguments()返回null。这也意味着你没有解决问题。现在String名称为null。
确保在添加片段时,您正在添加名为setArguments()的实例,而不是创建新实例。
像这样修改你的代码来处理异常:
Bundle b;
if ((b = getArguments()) != null) {
name = b.getString("name");
} else {
name = "";
Log.v(TAG, "No arguments set for NoteFragment")
}