无论我做什么,当我使用linearLayout.addView(t)时;我的应用程序崩溃并发出警告“Dividir a Conta stopped”。我没有想到什么是错的,我在谷歌搜索了很多页面,但没有一个解决了我的问题。
这是我的fragment_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
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="br.rodrigo.davy.vazdividiraconta.MainActivity$PlaceholderFragment" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/chooseEstablishment"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/LinearLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Teste"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</ScrollView>
这是我的代码:
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();
}
//listEstablishment.add(new Establishment("Bar da Maria"));
//listEstablishment.add(new Establishment("César Burguer"));
//listEstablishment.add(new Establishment("Kilogrego"));
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.LinearLayout2);
//for(Establishment e : listEstablishment)
//{
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView t = new TextView(this);
t.setLayoutParams(lparams);
t.setText("Teste 2");
linearLayout.addView(t);
//}
}
我的LogCat:
07-02 22:08:37.626: D/AndroidRuntime(3970): Shutting down VM
07-02 22:08:37.626: W/dalvikvm(3970): threadid=1: thread exiting with uncaught exception (group=0x41601ba8)
07-02 22:08:37.626: E/AndroidRuntime(3970): FATAL EXCEPTION: main
07-02 22:08:37.626: E/AndroidRuntime(3970): Process: br.rodrigo.davy.vazdividiraconta, PID: 3970
07-02 22:08:37.626: E/AndroidRuntime(3970): java.lang.RuntimeException: Unable to start activity ComponentInfo{br.rodrigo.davy.vazdividiraconta/br.rodrigo.davy.vazdividiraconta.MainActivity}: java.lang.NullPointerException
07-02 22:08:37.626: E/AndroidRuntime(3970): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
07-02 22:08:37.626: E/AndroidRuntime(3970): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
07-02 22:08:37.626: E/AndroidRuntime(3970): at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-02 22:08:37.626: E/AndroidRuntime(3970): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-02 22:08:37.626: E/AndroidRuntime(3970): at android.os.Handler.dispatchMessage(Handler.java:102)
07-02 22:08:37.626: E/AndroidRuntime(3970): at android.os.Looper.loop(Looper.java:136)
07-02 22:08:37.626: E/AndroidRuntime(3970): at android.app.ActivityThread.main(ActivityThread.java:5001)
07-02 22:08:37.626: E/AndroidRuntime(3970): at java.lang.reflect.Method.invokeNative(Native Method)
07-02 22:08:37.626: E/AndroidRuntime(3970): at java.lang.reflect.Method.invoke(Method.java:515)
07-02 22:08:37.626: E/AndroidRuntime(3970): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
07-02 22:08:37.626: E/AndroidRuntime(3970): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
07-02 22:08:37.626: E/AndroidRuntime(3970): at dalvik.system.NativeStart.main(Native Method)
07-02 22:08:37.626: E/AndroidRuntime(3970): Caused by: java.lang.NullPointerException
07-02 22:08:37.626: E/AndroidRuntime(3970): at br.rodrigo.davy.vazdividiraconta.MainActivity.onCreate(MainActivity.java:50)
07-02 22:08:37.626: E/AndroidRuntime(3970): at android.app.Activity.performCreate(Activity.java:5231)
07-02 22:08:37.626: E/AndroidRuntime(3970): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-02 22:08:37.626: E/AndroidRuntime(3970): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
07-02 22:08:37.626: E/AndroidRuntime(3970): ... 11 more
07-02 22:08:41.120: I/Process(3970): Sending signal. PID: 3970 SIG: 9
答案 0 :(得分:2)
LinearLayout2
位于 Fragment的布局内,而不是Activity的布局。所以基本上,你在错误的地方打电话给findViewById()
。
您有两种可能的解决方案:
onCreateView()
或