2014-04-26 78 views
0

好吧,我正在嘗試將ZXing意圖整合到我的Android應用程序中,我想我已經到了擁有我需要的所有東西,至少測試掃描儀並獲得某種輸出,但是,我被一個空指針異常錯誤阻止。NullPointerException錯誤扼殺了我的廢話

package com.example.scanmob; 

import android.support.v7.app.ActionBarActivity; 
import android.support.v7.app.ActionBar; 
import android.support.v4.app.Fragment; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.os.Build; 
import com.google.zxing.integration.android.IntentIntegrator; 
import com.google.zxing.integration.android.IntentResult; 
import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 

public class MainActivity extends ActionBarActivity implements OnClickListener { 

    private Button scanBtn; 
    private TextView formatTxt, contentTxt; 

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

     scanBtn = (Button)findViewById(R.id.scan_button); 
     formatTxt = (TextView)findViewById(R.id.scan_format); 
     contentTxt = (TextView)findViewById(R.id.scan_content); 

     scanBtn.setOnClickListener(this); 

     if (savedInstanceState == null) { 
      getSupportFragmentManager().beginTransaction() 
        .add(R.id.container, new PlaceholderFragment()).commit(); 
     } 
    } 

    public void onClick(View v){ 
     if(v.getId()==R.id.scan_button){ 
      IntentIntegrator scanIntegrator = new IntentIntegrator(this); 
      scanIntegrator.initiateScan(); 
     } 
    } 

    public void onActivityResult(int requestCode, int resultCode, Intent intent) { 
     IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent); 
     if (scanResult != null) { 
      String scanContent = scanResult.getContents(); 
      String scanFormat = scanResult.getFormatName(); 

      formatTxt.setText("FORMAT: " + scanFormat); 
      contentTxt.setText("CONTENT: " + scanContent); 
     } 
     else { 
      Toast toast = Toast.makeText(getApplicationContext(), 
       "No scan data received!", Toast.LENGTH_SHORT); 
      toast.show(); 
     } 
     // else continue with any other code you need in the method 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 

     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    /** 
    * A placeholder fragment containing a simple view. 
    */ 
    public static class PlaceholderFragment extends Fragment { 

     public PlaceholderFragment() { 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
       Bundle savedInstanceState) { 
      View rootView = inflater.inflate(R.layout.fragment_main, container, 
        false); 
      return rootView; 
     } 
    } 

} 

經過調試,我能夠得到精確的錯誤位置:

scanBtn = (Button)findViewById(R.id.scan_button); 
formatTxt = (TextView)findViewById(R.id.scan_format); 
contentTxt = (TextView)findViewById(R.id.scan_content); 

那些三線是空的,因此使用起來與setOnClickListener和的setText會導致應用程序崩潰。我只是不明白爲什麼這三行是空的。

這裏是fragment_main.xml樣子:

<RelativeLayout 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" > 
    <Button android:id="@+id/scan_button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:text="@string/scan" /> 

    <TextView 
     android:id="@+id/scan_format" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textIsSelectable="true" 
     android:layout_centerHorizontal="true" 
     android:layout_below="@id/scan_button" /> 
    <TextView 
     android:id="@+id/scan_content" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textIsSelectable="true" 
     android:layout_centerHorizontal="true" 
     android:layout_below="@id/scan_format" /> 

</RelativeLayout> 

以下是完整的錯誤,如果有人想了解:

04-25 21:25:27.759:d/AndroidRuntime(28663 ):關閉VM 04-25 21:25:27.759:W/dalvikvm(28663):threadid = 1:線程退出 未捕獲的異常(組= 0x413d2ac8)04-25 21:25:27.769: E/AndroidRuntime (28663):致命例外:main 04-25 21:25:27.769: E/Androi dRuntime(28663):java.lang.RuntimeException:無法啓動 活動 ComponentInfo {com.example.scanmob/com.example.scanmob.MainActivity}: java.lang.NullPointerException 04-25 21:25:27.769: E/AndroidRuntime(28663):在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2202) 04-25 21:25:27.769:E/AndroidRuntime(28663):在 android.app.ActivityThread.handleLaunchActivity( ActivityThread.java:2252) 04-25 21:25:27.769:E/AndroidRuntime(28663):在 android.app.ActivityThread.access $ 600(ActivityThread.java:146)04-25 21:25:27.769: E/AndroidRuntime(28663):在 android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1240) 04-25 21:25:27.769:E/AndroidRuntime(28663):at android.os.Handler.dispatchMessage(Handler.java:99)04-25 21:25:27.769:E/AndroidRuntime(28663)在 android.os.Looper.loop(Looper.java:137)04-25 21:25:27.769: E/AndroidRuntime(28663):在 android.app.ActivityThread.main(ActivityThread.java:5168)04 -25 21:25:27.769:E/AndroidRuntime(28663):在 java.lang.reflect.Method.invokeNative(Native Method)04-25 21:25:27.769:E/AndroidRuntime(28663):at java.lang.reflect.Method.invoke(Method.java:511)04-25 21:25:27.769: E/AndroidRuntime(28663):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit .java:797) 04-25 21:25:27.769:E/AndroidRuntime(28663):在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:564)04-25 21:25:27.769:E/AndroidRuntime (28663):在 dalvik.system.NativeStart.main(本機方法)04-25 21:25:27.769: E/AndroidRuntime(28663):由:顯示java.lang.NullPointerException 04-25 21:25: 27.769:E/AndroidRuntime(28663):at com.example.scanmob.MainActivity.onCreate(MainActivity.java:38)04-25 21:25:27.769:E/AndroidRuntime(28663):at android.app。 Activity.performCreate(Activity.java:5200)04-25 21:25:27.769:E/AndroidRuntime(28663):at android.app.Instrumentation。callActivityOnCreate(Instrumentation.java:1080) 04-25 21:25:27.769:E/AndroidRuntime(28663):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2166) 04-25 21:25:27.769 :E/AndroidRuntime(28663):... 11更多

誰能告訴我我做錯了什麼?

編輯activity_main.xml中的位置:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.scanmob.MainActivity" 
    tools:ignore="MergeRootFrame" /> 
+0

是您發佈的XML來自'activity_main.xml'? – AxiomaticNexus

回答

0

把這個四行Placeholderfragment類你的onCreateView(),並看看是否有幫助....

scanBtn = (Button)findViewById(R.id.scan_button); 
     formatTxt = (TextView)findViewById(R.id.scan_format); 
     contentTxt = (TextView)findViewById(R.id.scan_content); 

     scanBtn.setOnClickListener(this);