I am creating an app that it is throwing an error, activity is not started. My activity class is:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.videos_layout);
i = new FirstImage(this);
c = (ViewFlipper) findViewById(R.id.viewFlipper3);
back = (Button) findViewById(R.id.button1);
next = (Button) findViewById(R.id.button2);
t = (TextView) findViewById(R.id.textView1);
if(count == 0)
i.changeImage(R.drawable.human);
c.addView(i);
c.setOnTouchListener(this);
c.onTouchEvent(event);
addListenerOnButton();
}
private void addListenerOnButton() {
if (count == 0)
back.setEnabled(false);
next.setOnClickListener( new OnClickListener() {
public void onClick(View arg0) {
if(count==0){
i.changeImage(R.drawable.hand);
back.setEnabled(true);
count++;
addListenerOnButton();
}
else if (count==1){
i.changeImage(R.drawable.tissue);
count++;
addListenerOnButton();
}
else if (count==2){
image.setImageResource(R.drawable.cellconstfinal1);
count++;
addListenerOnButton();
}
else if (count==3){
//nucleus
image.setImageResource(R.drawable.cellconstfinal1);
count++;
addListenerOnButton();
}
else if (count==4){
//nchromosome
image.setImageResource(R.drawable.hand);
count++;
addListenerOnButton();
}
else
{ //double helix
count++;
image.setImageResource(R.drawable.handash1);
next.setEnabled(false);
addListenerOnButton();
}
}
});
back.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if(count==1){
image.setImageResource(R.drawable.human);
back.setEnabled(false);
count--;
addListenerOnButton();
}
else if (count==2){
image.setImageResource(R.drawable.hand);
count--;
addListenerOnButton();
}
else if (count==3){
image.setImageResource(R.drawable.tissue);
count--;
addListenerOnButton();
}
else if (count==4){
image.setImageResource(R.drawable.cellconstfinal1);
count--;
addListenerOnButton();
}
else if (count==5){
//nucleus
image.setImageResource(R.drawable.tissue);
count--;
addListenerOnButton();
}
else {
//chromosome
//count==6
image.setImageResource(R.drawable.cellconstfinal1);
count--;
addListenerOnButton();
next.setEnabled(true);
}
}
});
}
public void pageinfo(float a,float b){
t.setText(Float.toString(a)+"x"+Float.toString(b)+"y");
}
public boolean onTouch(View v, MotionEvent me) {
// TODO Auto-generated method stub
switch(me.getAction()){
case MotionEvent.ACTION_DOWN:
a=me.getX();
b= me.getY();
pageinfo(a,b);
break;
case MotionEvent.ACTION_MOVE:
a=me.getX();
b= me.getY();
pageinfo(a,b);
break;
case MotionEvent.ACTION_UP:
a=me.getX();
b= me.getY();
pageinfo(a,b);
break;
case MotionEvent.ACTION_OUTSIDE:
a=me.getX();
b= me.getY();
pageinfo(a,b);
break;
default: return false;
}
return true;
}
}
My FirstImage class code:
public class FirstImage extends ImageView {
float a=0;
float b=0;
TextView t;
ImageView i;
public FirstImage(Context context) {
super(context);
t= (TextView) findViewById(R.id.textView1);
i= new ImageView (context);
}
public FirstImage(Context context, AttributeSet attrs) {
super(context, attrs);
}
protected void changeImage(int id){
i.setImageResource(id);
}
The stacktrace with the exception:
05-11 00:21:41.197: E/AndroidRuntime(1142): FATAL EXCEPTION: main
05-11 00:21:41.197: E/AndroidRuntime(1142): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidtablayout/com.example.androidtablayout.VideosActivity}: java.lang.NullPointerException
05-11 00:21:41.197: E/AndroidRuntime(1142): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-11 00:21:41.197: E/AndroidRuntime(1142): at android.app.ActivityThread.startActivityNow(ActivityThread.java:2503)
05-11 00:21:41.197: E/AndroidRuntime(1142): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
05-11 00:21:41.197: E/AndroidRuntime(1142): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
05-11 00:21:41.197: E/AndroidRuntime(1142): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:651)
05-11 00:21:41.197: E/AndroidRuntime(1142): at android.widget.TabHost.setCurrentTab(TabHost.java:323)
05-11 00:21:41.197: E/AndroidRuntime(1142): at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:129)
05-11 00:21:41.197: E/AndroidRuntime(1142): at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:453)
05-11 00:21:41.197: E/AndroidRuntime(1142): at android.view.View.performClick(View.java:2408)
05-11 00:21:41.197: E/AndroidRuntime(1142): at android.view.View$PerformClick.run(View.java:8816)
05-11 00:21:41.197: E/AndroidRuntime(1142): at android.os.Handler.handleCallback(Handler.java:587)
05-11 00:21:41.197: E/AndroidRuntime(1142): at android.os.Handler.dispatchMessage(Handler.java:92)
05-11 00:21:41.197: E/AndroidRuntime(1142): at android.os.Looper.loop(Looper.java:123)
05-11 00:21:41.197: E/AndroidRuntime(1142): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-11 00:21:41.197: E/AndroidRuntime(1142): at java.lang.reflect.Method.invokeNative(Native Method)
05-11 00:21:41.197: E/AndroidRuntime(1142): at java.lang.reflect.Method.invoke(Method.java:521)
05-11 00:21:41.197: E/AndroidRuntime(1142): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-11 00:21:41.197: E/AndroidRuntime(1142): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-11 00:21:41.197: E/AndroidRuntime(1142): at dalvik.system.NativeStart.main(Native Method)
05-11 00:21:41.197: E/AndroidRuntime(1142): Caused by: java.lang.NullPointerException
05-11 00:21:41.197: E/AndroidRuntime(1142): at com.example.androidtablayout.VideosActivity.onCreate(VideosActivity.java:45)
05-11 00:21:41.197: E/AndroidRuntime(1142): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-11 00:21:41.197: E/AndroidRuntime(1142): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-11 00:21:41.197: E/AndroidRuntime(1142): ... 18 more
You should always initialize the views in the
onCreatemethod:Also in the
FirstImageclass initialize thoseViewsin the constructor:Edit:
My advice is to follow some tutorials for custom
Views. You can’t add otherViewsto anImageViewsubclass(and you shouldn’t). Even if you build theImageViewin theFirstImagethechangeImagemethod will throw aNullPointerExceptionthe way your code is currently built.A simple custom view: