NullPointerException错误导致我的废话

时间:2014-04-26 01:26:44

标签: java android eclipse zxing

好的,我试图将ZXing意图集成到我的Android应用程序中,我想我已经达到了我需要的一切,至少测试扫描仪并得到某种输出但是,我被一个空指针异常错误所阻碍。

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);

这三行是​​null,因此将它们与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):致命异常:主04-25 21:25:27.769:   E / AndroidRuntime(28663):java.lang.RuntimeException:无法启动   活动   ComponentInfo {com.example.scanmob / com.example.scanmob.MainActivity}:   java.lang.NullPointerException 04-25 21:25:27.769:   E / AndroidRuntime(28663):at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2202)   04-25 21:25:27.769:E / AndroidRuntime(28663):at   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2252)   04-25 21:25:27.769:E / AndroidRuntime(28663):at   android.app.ActivityThread.access $ 600(ActivityThread.java:146)04-25   21:25:27.769:E / AndroidRuntime(28663):at   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):at   android.os.Looper.loop(Looper.java:137)04-25 21:25:27.769:   E / AndroidRuntime(28663):at   android.app.ActivityThread.main(ActivityThread.java:5168)04-25   21:25:27.769:E / AndroidRuntime(28663):at   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):at   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:564)04-25   21:25:27.769:E / AndroidRuntime(28663):at   dalvik.system.NativeStart.main(Native Method)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更多

谁能告诉我我做错了什么?

EDIT 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" />

1 个答案:

答案 0 :(得分: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);