2014-11-16 117 views
0

我想製作一個應用程序,您可以製作圖片並保存它們,但是當我測試它時,我的logcat中出現了這個錯誤:「java.lang.RuntimeException:無法啓動活動ComponentInfo {com.example.random/com.example.random.FotoMaker}:java.lang.NullPointerException「。我認爲問題與意圖有關,但我不知道我需要在FotoMaker.java中添加/更改什麼。無法啓動活動ComponentInfo NullpointerException -

MenuScreen.java:

package com.example.random; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.Window; 
import android.widget.Button; 

public class MenuScreen extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.activity_main); 



     findViewById(R.id.test).setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Log.d("DEBUG", "test"); 
       Intent intent = new Intent(MenuScreen.this, FotoMaker.class); 
       startActivityForResult(intent, 0); 
      } 
     }); 


     findViewById(R.test1).setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       Log.d("DEBUG", "test1"); 
       Intent intent = new Intent(MenuScreen.this, FotoMaker.class); 

      } 
     }); 

     findViewById(R.test2).setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       Log.d("DEBUG", "test2"); 
       Intent intent = new Intent(MenuScreen.this, FotoMaker.class); 

      } 
     }); 



     findViewById(R.id.verlaat_app).setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       Log.d("DEBUG", "test3"); 
       MenuScreen.this.finish(); 
      } 
     }); 
    } 
} 

MenuScreen.java:

package com.example.random; 


import android.app.Activity; 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.ImageView; 


public class FotoMaker extends Activity 
    { 
    ImageView iv; 


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

     iv = (ImageView) findViewById(R.id.imageView); 
     Button btn = (Button) findViewById(R.id.testpic); 
     btn.setOnClickListener(new OnClickListener() 
     { 
      @Override 
      public void onClick (View v){ 
       Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
       startActivityForResult(intent, 0); 

      } 
     }); 
     } 
     @Override 
     protected void onActivityResult(int requestCode, int resultCode, Intent data) 
     { 
      if(requestCode == 0) 
      { 
       Bitmap theImage = (Bitmap) data.getExtras().get("data"); 
       iv.setImageBitmap(theImage); 
      } 
     } 

    } 

logcat的:

11-16 18:30:14.366: E/AndroidRuntime(1522): FATAL EXCEPTION: main 
11-16 18:30:14.366: E/AndroidRuntime(1522): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.random/com.example.random.FotoMaker}: java.lang.NullPointerException 
11-16 18:30:14.366: E/AndroidRuntime(1522):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1996) 
11-16 18:30:14.366: E/AndroidRuntime(1522):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2023) 
11-16 18:30:14.366: E/AndroidRuntime(1522):  at android.app.ActivityThread.access$600(ActivityThread.java:127) 
11-16 18:30:14.366: E/AndroidRuntime(1522):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1174) 
11-16 18:30:14.366: E/AndroidRuntime(1522):  at android.os.Handler.dispatchMessage(Handler.java:99) 
11-16 18:30:14.366: E/AndroidRuntime(1522):  at android.os.Looper.loop(Looper.java:137) 
11-16 18:30:14.366: E/AndroidRuntime(1522):  at android.app.ActivityThread.main(ActivityThread.java:4503) 
11-16 18:30:14.366: E/AndroidRuntime(1522):  at java.lang.reflect.Method.invokeNative(Native Method) 
11-16 18:30:14.366: E/AndroidRuntime(1522):  at java.lang.reflect.Method.invoke(Method.java:511) 
11-16 18:30:14.366: E/AndroidRuntime(1522):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809) 
11-16 18:30:14.366: E/AndroidRuntime(1522):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576) 
11-16 18:30:14.366: E/AndroidRuntime(1522):  at dalvik.system.NativeStart.main(Native Method) 
11-16 18:30:14.366: E/AndroidRuntime(1522): Caused by: java.lang.NullPointerException 
11-16 18:30:14.366: E/AndroidRuntime(1522):  at com.example.random.FotoMaker.onCreate(FotoMaker.java:27) 
11-16 18:30:14.366: E/AndroidRuntime(1522):  at android.app.Activity.performCreate(Activity.java:4479) 
11-16 18:30:14.366: E/AndroidRuntime(1522):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050) 
11-16 18:30:14.366: E/AndroidRuntime(1522):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1960) 
11-16 18:30:14.366: E/AndroidRuntime(1522):  ... 11 more 

testpic.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <ImageView 
     android:id="@+id/imageView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:src="@drawable/ic_launcher" /> 

</LinearLayout> 
+0

後testpic XML文件還 –

+0

我加入了testpic.xml – goedkoop

+0

我沒有看到任何'Button'在'testpic.xml'但您要設置一個按鈕,ID爲'testpic' –

回答

2

似乎你忘記在你的xml佈局中添加testpic按鈕ID,因此在FotoMaker活動你的按鈕btn爲空。

Button btn = (Button) findViewById(R.id.testpic); <-- null here 

所以ID爲testpictestpic.xml佈局文件添加按鈕視圖。

+0

另一個問題是:當我按下按鈕test/test1/test2,沒有任何反應,爲什麼? – goedkoop

1
Button btn = (Button) findViewById(R.id.testpic); 

沒有按鍵稱爲testpic佈局「testpic」 你需要添加一個按鈕testpic你的球鐵QT500佈局文件,否則將返回null。

相關問題