The app force closes without even starting, see below for logcat info and (app works when 3 lines below are commented out)
NullPointerException
but I don't know where and how to solve it.XML file:
<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.figurehowtodo.MainActivity$PlaceholderFragment" >
<TextView
android:id="@+id/produceText1"
android:layout_width="280dp"
android:layout_height="50dp"
android:text="@string/hello_world"
/>
<Button
android:id="@+id/myFirstButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ClickMe1" />
<Button
android:id="@+id/mySecondButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ClickMe2" />
<Button
android:id="@+id/myThirdButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ClickMe3" />
</LinearLayout>
MainActivity.java
package com.example.figurehowtodo;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
Button myFirstButton;
Button mySecondButton;
Button myThirdButton;
TextView tvView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
myFirstButton = (Button) findViewById(R.id.myFirstButton);
mySecondButton = (Button) findViewById(R.id.mySecondButton);
myThirdButton = (Button) findViewById(R.id.myThirdButton);
tvView = (TextView) findViewById(R.id.produceText1);
//if (tvView == null) { Log.w("", "TextView is null"); }
If I comment out the bottom three lines my app runs (but without any button functionality). I think the "new" might have something to do with it?
myFirstButton.setOnClickListener(new MyOwnOnClickListener(tvView, "myFirstButton"));
mySecondButton.setOnClickListener(new MyOwnOnClickListener(tvView, "mySecondButton"));
myThirdButton.setOnClickListener(new MyOwnOnClickListener(tvView, "myThirdButton"));
MyOwnOnClickListner.java
package com.example.figurehowtodo;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MyOwnOnClickListener extends Activity implements OnClickListener{
//int id; //comment line out in order to make it work
//TextView id3;
/*
*
*MainActivity caller;
*public MyOwnOnClickListener() {
*€ addiTion();
* IGNORE THIS BIT
*}
*
*/
TextView outputBoxId;
String re_id_button_name;
Button myFirstButton;
Button mySecondButton;
Button myThirdButton;
TextView tvView3;
public MyOwnOnClickListener(TextView id2, String id) {
this.outputBoxId = id2;
this.re_id_button_name = id;
myFirstButton = (Button) findViewById(R.id.myFirstButton);
mySecondButton = (Button) findViewById(R.id.mySecondButton);
myThirdButton = (Button) findViewById(R.id.myThirdButton);
}
public void onClick(View re_id_button_name) {
//tvView3 = (TextView) findViewById(R.id.produceText1);
outputBoxId.setText("it worked!!!");
}
/*
* -------IGNORE---------
if(re_id_button_name.equals(myFirstButton)){
addiTion();
}else{return;}
public void addiTion(){
//id = v.getId(); //comment line out in order to make it work
outputBoxId.setText("YOU CLICKED THE FIRST BUTTON!");
}
-------IGNORE---------*/
}
LogCat:
03-28 21:56:24.388: E/AndroidRuntime(1256): FATAL EXCEPTION: main
03-28 21:56:24.388: E/AndroidRuntime(1256): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.figurehowtodo/com.example.figurehowtodo.MainActivity}: java.lang.NullPointerException
03-28 21:56:24.388: E/AndroidRuntime(1256): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-28 21:56:24.388: E/AndroidRuntime(1256): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-28 21:56:24.388: E/AndroidRuntime(1256): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-28 21:56:24.388: E/AndroidRuntime(1256): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-28 21:56:24.388: E/AndroidRuntime(1256): at android.os.Handler.dispatchMessage(Handler.java:99)
03-28 21:56:24.388: E/AndroidRuntime(1256): at android.os.Looper.loop(Looper.java:123)
03-28 21:56:24.388: E/AndroidRuntime(1256): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-28 21:56:24.388: E/AndroidRuntime(1256): at java.lang.reflect.Method.invokeNative(Native Method)
03-28 21:56:24.388: E/AndroidRuntime(1256): at java.lang.reflect.Method.invoke(Method.java:507)
03-28 21:56:24.388: E/AndroidRuntime(1256): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-28 21:56:24.388: E/AndroidRuntime(1256): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-28 21:56:24.388: E/AndroidRuntime(1256): at dalvik.system.NativeStart.main(Native Method)
03-28 21:56:24.388: E/AndroidRuntime(1256): Caused by: java.lang.NullPointerException
03-28 21:56:24.388: E/AndroidRuntime(1256): at android.app.Activity.findViewById(Activity.java:1647)
03-28 21:56:24.388: E/AndroidRuntime(1256): at com.example.figurehowtodo.MyOwnOnClickListener.<init>(MyOwnOnClickListener.java:31)
03-28 21:56:24.388: E/AndroidRuntime(1256): at com.example.figurehowtodo.MainActivity.onCreate(MainActivity.java:25)
03-28 21:56:24.388: E/AndroidRuntime(1256): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-28 21:56:24.388: E/AndroidRuntime(1256): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-28 21:56:24.388: E/AndroidRuntime(1256): ... 11 more
03-28 21:56:24.408: W/ActivityManager(62): Force finishing activity com.example.figurehowtodo/.MainActivity
03-28 21:56:24.917: W/ActivityManager(62): Activity pause timeout for HistoryRecord{407eda90 com.example.figurehowtodo/.MainActivity}
03-28 21:56:30.757: D/dalvikvm(233): GC_EXPLICIT freed 8K, 55% free 2597K/5703K, external 1625K/2137K, paused 53ms
The main problem here is that your custom OnClickListener
shouldn't extends Activity
because it isn't an Activity
. And such, it doesn't have the layout
that holds the id
s of the View
s you are trying to inflate.
This is why it crashes because you get a NPE
when trying to set the listener
on those View
s. You need to set the listeners
on them in your actual Activity
where you inflate the layout
with findViewById()
.
So, instead of having something like
myBtn.setOnClickListener(new View.OnClickListener());
you would have
myBtn.setOnClickListener(new MyOwnOnClickListener());
Edit
Try this, use the Context
from the View
passed in to get the other View
. I'm not positive this will work and I don't have time to test it at this moment but let's see.
public MyOwnOnClickListener(TextView id2, String id) {
myFirstButton = (Button) id2.getContext().findViewById(R.id.myFirstButton);
}