-4

我遵循此tutorial並創建了一個簡單的條碼掃描器。但是由於下線,我的應用程序在啓動時會停止。爲什麼Android應用程序在使用setOnClickListener(this)時會停止?

scanBtn.setOnClickListener(this); 

如果我註釋掉這一行的應用程序不會停止。請讓我知道如何解決這個問題。

謝謝。

package com.sample.barcodescanningapp; 

import android.app.Activity; 
import android.app.Fragment; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.google.zxing.integration.android.IntentIntegrator; 
import com.google.zxing.integration.android.IntentResult; 

public class MainActivity extends Activity implements OnClickListener { 

    private Button scanBtn; 
    private TextView formatTxt, contentTxt; 

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

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

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

     scanBtn.setOnClickListener(this); 

    } 

    @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; 
     } 

    } 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     if (v.getId() == R.id.scan_button) { 

      IntentIntegrator scanIntegrator = new IntentIntegrator(this); 
      scanIntegrator.initiateScan(); 
     } 
    } 

    public void onActivityResult(int requestCode, int resultCode, Intent intent) { 
     // retrieve scan result 
     IntentResult scanningResult = IntentIntegrator.parseActivityResult(
       requestCode, resultCode, intent); 

     if (scanningResult != null) { 
      // we have a result 
      String scanContent = scanningResult.getContents(); 
      String scanFormat = scanningResult.getFormatName(); 

      formatTxt.setText("FORMAT : " + scanFormat); 
      contentTxt.setText("CONTENT : " + scanContent); 

     } else { 
      Toast toast = Toast.makeText(getApplicationContext(), 
        "No scan data received!", Toast.LENGTH_SHORT); 
      toast.show(); 
     } 
    } 

} 

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.sample.barcodescanningapp.MainActivity" 
    tools:ignore="MergeRootFrame" /> 

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" 
    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.sample.barcodescanningapp.MainActivity$PlaceholderFragment" > 

    <Button 
     android:id="@+id/scan_button" 
     android:layout_width="match_parent" 
     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:layout_alignParentLeft="true" 
     android:layout_below="@id/scan_button" 
     android:text="FORMAT : " 
     android:textIsSelectable="true" /> 

    <TextView 
     android:id="@+id/scan_content" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@id/scan_format" 
     android:text="CONTENT : " 
     android:textIsSelectable="true" /> 

</RelativeLayout> 

logcat的

01-27 00:37:05.728: E/AndroidRuntime(3970): FATAL EXCEPTION: main 
01-27 00:37:05.728: E/AndroidRuntime(3970): Process: com.sample.barcodescanningapp, PID: 3970 
01-27 00:37:05.728: E/AndroidRuntime(3970): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sample.barcodescanningapp/com.sample.barcodescanningapp.MainActivity}: java.lang.NullPointerException 
01-27 00:37:05.728: E/AndroidRuntime(3970):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2190) 
01-27 00:37:05.728: E/AndroidRuntime(3970):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2239) 
01-27 00:37:05.728: E/AndroidRuntime(3970):  at android.app.ActivityThread.access$800(ActivityThread.java:141) 
01-27 00:37:05.728: E/AndroidRuntime(3970):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202) 
01-27 00:37:05.728: E/AndroidRuntime(3970):  at android.os.Handler.dispatchMessage(Handler.java:102) 
01-27 00:37:05.728: E/AndroidRuntime(3970):  at android.os.Looper.loop(Looper.java:136) 
01-27 00:37:05.728: E/AndroidRuntime(3970):  at android.app.ActivityThread.main(ActivityThread.java:5045) 
01-27 00:37:05.728: E/AndroidRuntime(3970):  at java.lang.reflect.Method.invokeNative(Native Method) 
01-27 00:37:05.728: E/AndroidRuntime(3970):  at java.lang.reflect.Method.invoke(Method.java:515) 
01-27 00:37:05.728: E/AndroidRuntime(3970):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
01-27 00:37:05.728: E/AndroidRuntime(3970):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602) 
01-27 00:37:05.728: E/AndroidRuntime(3970):  at dalvik.system.NativeStart.main(Native Method) 
01-27 00:37:05.728: E/AndroidRuntime(3970): Caused by: java.lang.NullPointerException 
01-27 00:37:05.728: E/AndroidRuntime(3970):  at com.sample.barcodescanningapp.MainActivity.onCreate(MainActivity.java:39) 
01-27 00:37:05.728: E/AndroidRuntime(3970):  at android.app.Activity.performCreate(Activity.java:5256) 
01-27 00:37:05.728: E/AndroidRuntime(3970):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
01-27 00:37:05.728: E/AndroidRuntime(3970):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2154) 
01-27 00:37:05.728: E/AndroidRuntime(3970):  ... 11 more 
+0

哪裏是你的XML文件中使用呢?和logcat –

+0

我沒有使用任何XML。作爲本教程解釋我創建了一個打包並創建了2個類裏面 – Sachith

+0

把代碼的activity_main.xml –

回答

1

您需要聲明和fragmentMainActivity

   package com.sample.barcodescanningapp; 

       import android.app.Activity; 
       import android.app.Fragment; 
       import android.content.Intent; 
       import android.os.Bundle; 
       import android.view.LayoutInflater; 
       import android.view.Menu; 
       import android.view.MenuItem; 
       import android.view.View; 
       import android.view.View.OnClickListener; 
       import android.view.ViewGroup; 
       import android.widget.Button; 
       import android.widget.TextView; 
       import android.widget.Toast; 

       import com.google.zxing.integration.android.IntentIntegrator; 
       import com.google.zxing.integration.android.IntentResult; 



       public class MainActivity extends Activity { 



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

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



        } 

        @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 { 
private Button scanBtn; 
       private TextView formatTxt, contentTxt; 

         public PlaceholderFragment() { 
         } 

         @Override 
         public View onCreateView(LayoutInflater inflater, ViewGroup container, 
           Bundle savedInstanceState) { 
          View rootView = inflater.inflate(R.layout.fragment_main, container, 
            false); 
      scanBtn = (Button) rootView .findViewById(R.id.scan_button); 
         formatTxt = (TextView) rootView .findViewById(R.id.scan_format); 
         contentTxt = (TextView) rootView .findViewById(R.id.scan_content); 

         scanBtn.setOnClickListener(new View.OnClickListener() { 

        @Override 
        public void onClick(View v) { 
         // TODO Auto-generated method stub 

        } 
       }); 
          return rootView; 
         } 

        } 



        public void onActivityResult(int requestCode, int resultCode, Intent intent) { 
         // retrieve scan result 
         IntentResult scanningResult = IntentIntegrator.parseActivityResult(
           requestCode, resultCode, intent); 

         if (scanningResult != null) { 
          // we have a result 
          String scanContent = scanningResult.getContents(); 
          String scanFormat = scanningResult.getFormatName(); 

          formatTxt.setText("FORMAT : " + scanFormat); 
          contentTxt.setText("CONTENT : " + scanContent); 

         } else { 
          Toast toast = Toast.makeText(getApplicationContext(), 
            "No scan data received!", Toast.LENGTH_SHORT); 
          toast.show(); 
         } 
        } 

       } 
+0

但我得到了這個錯誤「無法對類型爲Activity」的非靜態方法findViewById(int)進行靜態引用 – Sachith

+0

查看更新的代碼 –

1

添加按鈕和TextView的對象,然後單擊PlaceholderFragment這樣的方法調用,

public static class PlaceholderFragment extends Fragment implements OnClickListener { 

    public PlaceholderFragment() { 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_main, container, 
       false); 
     scanBtn = (Button) rootView.findViewById(R.id.scan_button); 
     formatTxt = (TextView) rootView.findViewById(R.id.scan_format); 
     contentTxt = (TextView) rootView.findViewById(R.id.scan_content); 

     scanBtn.setOnClickListener(this); 

     return rootView; 
    } 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     if (v.getId() == R.id.scan_button) { 
       IntentIntegrator scanIntegrator = new IntentIntegrator(this); 
      scanIntegrator.initiateScan(); 
     } 
    } 
} 
+0

但我得到這個錯誤「無法從類型活動」的靜態引用非靜態方法findViewById(INT) – Sachith

+0

從PlaceholderFragment的類declation刪除靜態或使私人按鈕scanBtn; private TextView formatTxt,contentTxt;靜態 –

+0

@Sachith,解決? –

相關問題