gpt4 book ai didi

java - Android Fragment 导致 NullPointerException

转载 作者:行者123 更新时间:2023-12-02 06:05:33 24 4
gpt4 key购买 nike

首先要说的是,我知道有很多类似的帖子,并且我看过它们,但是我对 Android 开发很陌生,还不够熟悉它,无法修改其他答案来修复我的确切问题问题。

我有一个默认使用包含按钮和 TextView 的新项目创建的 fragment ,并且在我的 MainActivity.java 中我为该按钮创建了一个 onClick 函数。该函数抛出 NPE。这是 LogCat:

03-10 19:26:52.620: D/AndroidRuntime(878): Shutting down VM
03-10 19:26:52.620: W/dalvikvm(878): threadid=1: thread exiting with uncaught exception (group=0xb2af2ba8)
03-10 19:26:52.630: E/AndroidRuntime(878): FATAL EXCEPTION: main
03-10 19:26:52.630: E/AndroidRuntime(878): Process: com.example.main, PID: 878
03-10 19:26:52.630: E/AndroidRuntime(878): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.main/com.example.main.MainActivity}: java.lang.NullPointerException
03-10 19:26:52.630: E/AndroidRuntime(878): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-10 19:26:52.630: E/AndroidRuntime(878): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-10 19:26:52.630: E/AndroidRuntime(878): at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-10 19:26:52.630: E/AndroidRuntime(878): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-10 19:26:52.630: E/AndroidRuntime(878): at android.os.Handler.dispatchMessage(Handler.java:102)
03-10 19:26:52.630: E/AndroidRuntime(878): at android.os.Looper.loop(Looper.java:136)
03-10 19:26:52.630: E/AndroidRuntime(878): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-10 19:26:52.630: E/AndroidRuntime(878): at java.lang.reflect.Method.invokeNative(Native Method)
03-10 19:26:52.630: E/AndroidRuntime(878): at java.lang.reflect.Method.invoke(Method.java:515)
03-10 19:26:52.630: E/AndroidRuntime(878): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-10 19:26:52.630: E/AndroidRuntime(878): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-10 19:26:52.630: E/AndroidRuntime(878): at dalvik.system.NativeStart.main(Native Method)
03-10 19:26:52.630: E/AndroidRuntime(878): Caused by: java.lang.NullPointerException
03-10 19:26:52.630: E/AndroidRuntime(878): at com.example.main.MainActivity.onCreate(MainActivity.java:31)
03-10 19:26:52.630: E/AndroidRuntime(878): at android.app.Activity.performCreate(Activity.java:5231)
03-10 19:26:52.630: E/AndroidRuntime(878): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-10 19:26:52.630: E/AndroidRuntime(878): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-10 19:26:52.630: E/AndroidRuntime(878): ... 11 more
03-10 19:31:54.292: I/Process(878): Sending signal. PID: 878 SIG: 9

MainActivity.java:

package com.example.main;

import android.support.v7.app.ActionBarActivity;
...

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}

final TextView myText = (TextView) findViewById(R.id.text1);
Button myButton = (Button) findViewById(R.id.button1);

myButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
myText.setText("Yes!");
}
});
}

@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;
}
}

}

最佳答案

如果TextViewButton位于布局的fragment中,则需要在fragment的onCreatedView方法中找到它们的id。然后,你应该这样做:

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);
final TextView myText = (TextView) rootView.findViewById(R.id.text1);
Button myButton = (Button) rootView.findViewById(R.id.button1);
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myText.setText("Yes!");
}
});
return rootView;
}
}

关于java - Android Fragment 导致 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22313714/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com