I always get this error when i try to run my own programmed App.
It’s just a trying App which should on Button click add a TextView.
I have no idea where the problem is. There are no Errors and no Warnings in the Sourcecode.
Here you can see the logcat:
01-11 07:52:02.219: E/AndroidRuntime(698): FATAL EXCEPTION: main
01-11 07:52:02.219: E/AndroidRuntime(698): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{local.roger.AddWidget/local.roger.AddWidget.AddWidgetActivity}: java.lang.NullPointerException
01-11 07:52:02.219: E/AndroidRuntime(698): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1879)
01-11 07:52:02.219: E/AndroidRuntime(698): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
01-11 07:52:02.219: E/AndroidRuntime(698): at android.app.ActivityThread.access$600(ActivityThread.java:122)
01-11 07:52:02.219: E/AndroidRuntime(698): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
01-11 07:52:02.219: E/AndroidRuntime(698): at android.os.Handler.dispatchMessage(Handler.java:99)
01-11 07:52:02.219: E/AndroidRuntime(698): at android.os.Looper.loop(Looper.java:137)
01-11 07:52:02.219: E/AndroidRuntime(698): at android.app.ActivityThread.main(ActivityThread.java:4340)
01-11 07:52:02.219: E/AndroidRuntime(698): at java.lang.reflect.Method.invokeNative(Native Method)
01-11 07:52:02.219: E/AndroidRuntime(698): at java.lang.reflect.Method.invoke(Method.java:511)
01-11 07:52:02.219: E/AndroidRuntime(698): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-11 07:52:02.219: E/AndroidRuntime(698): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-11 07:52:02.219: E/AndroidRuntime(698): at dalvik.system.NativeStart.main(Native Method)
01-11 07:52:02.219: E/AndroidRuntime(698): Caused by: java.lang.NullPointerException
01-11 07:52:02.219: E/AndroidRuntime(698): at android.app.Activity.findViewById(Activity.java:1794)
01-11 07:52:02.219: E/AndroidRuntime(698): at local.roger.AddWidget.AddWidgetActivity.<init>(AddWidgetActivity.java:15)
01-11 07:52:02.219: E/AndroidRuntime(698): at java.lang.Class.newInstanceImpl(Native Method)
01-11 07:52:02.219: E/AndroidRuntime(698): at java.lang.Class.newInstance(Class.java:1319)
01-11 07:52:02.219: E/AndroidRuntime(698): at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
01-11 07:52:02.219: E/AndroidRuntime(698): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1870)
01-11 07:52:02.219: E/AndroidRuntime(698): ... 11 more
The main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btn_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_add" />
<LinearLayout
android:id="@+id/ll_vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
The Source Code:
package local.roger.AddWidget;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;
public class AddWidgetActivity extends Activity {
/** Called when the activity is first created. */
int counter = 0;
LinearLayout ll_vertical = (LinearLayout)findViewById(R.id.ll_vertical);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn_add = (Button)findViewById(R.id.btn_add);
btn_add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v){
createTextView("TestText_"+counter);
}
});
}
public void createTextView(String value) {
TextView txt_test = new TextView(this);
txt_test.setText(value);
txt_test.setId(counter);
counter = counter + 1;
txt_test.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
ll_vertical.addView(txt_test);
}
}
Thanks for helping
Regards Roger
First declare the
ll_verticalvariable:LinearLayout ll_vertical ;and thenput the code
ll_vertical = (LinearLayout)findViewById(R.id.ll_vertical);inside theonCreate()method, aftersetContentView(R.layout.main);So now the codes look like:
Before you inflate a layout file by calling setContentView(), the system can’t find the items in that layout file.That’s why the NullPointerException comes out.