1

大家好,这是过去两天第二次遇到这个问题。所以我想代码中有一些地方会出现可怕的错误。无论如何,我在这里尝试做的只是调用另一个类中的一个方法,该方法将在我的活动类中显示一个特定的文本文件。

MainActivity 是:

package com.example.testflashfile;

public class MainActivity extends Activity{
    Button nextButton;
    Button playButton;
    Button backButton;
    ReadText readText=new ReadText(this);
    Context contextObject;
    GestureDetector gestureDetector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
        setContentView(R.layout.activity_main);

        TextView helloTxt = (TextView)findViewById(R.id.displaytext);

        helloTxt.setText(readText.readTxt());

        gestureDetector = new GestureDetector(this.getApplicationContext(),new MyGestureDetector());
        View mainview = (View) findViewById(R.id.mainView);

        // Set the touch listener for the main view to be our custom gesture listener
        mainview.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if (gestureDetector.onTouchEvent(event)) {
                    return true;
                }
                return false;
            }
        });

        playButton=(Button)findViewById(R.id.play);
        playButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent startAnimation=new Intent(MainActivity.this,PlayAnimationActivity.class);
                startAnimation.putExtra("SWF_NAME","a");
                startActivity(startAnimation);
            }
        });

        nextButton=(Button)findViewById(R.id.next);
        nextButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent=new Intent(MainActivity.this,SecondActivity.class);
                startActivity(intent);

            }
        });

        backButton=(Button)findViewById(R.id.back);
        backButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent=new Intent(MainActivity.this,FifthActivity.class);
                startActivity(intent);

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    public class MyGestureDetector extends SimpleOnGestureListener   {

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            if (Math.abs(e1.getY() - e2.getY()) > GlobalVariables.SWIPE_MAX_OFF_PATH) {
                return false;
            }

            // right to left swipe
            if(e1.getX() - e2.getX() > GlobalVariables.SWIPE_MIN_DISTANCE && Math.abs(velocityX) > GlobalVariables.SWIPE_THRESHOLD_VELOCITY) {

                Intent i= new Intent(MainActivity.this,SecondActivity.class);
                startActivity(i);

                //  left to right  swipe
            }  else if (e2.getX() - e1.getX() > GlobalVariables.SWIPE_MIN_DISTANCE && Math.abs(velocityX) > GlobalVariables.SWIPE_THRESHOLD_VELOCITY) {


                Intent i= new Intent(MainActivity.this,FifthActivity.class);
                startActivity(i);

            }

            return false;
        }

        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }
    }
}

readTxt() 方法在下面的类中定义:

package com.example.testflashfile;

public class ReadText {
    Context context;
    public ReadText(Context c) {
        context = c;
        readTxt();
    }

    public String readTxt() {
        InputStream inputStream = context.getResources().openRawResource(R.raw.textone);

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

        int i;
        try {
            i = inputStream.read();
            while (i != -1)
            {
                byteArrayOutputStream.write(i);
                i = inputStream.read();
            }

            inputStream.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return byteArrayOutputStream.toString();
    } 
}

清单文件是:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.testflashfile"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:hardwareAccelerated="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.testflashfile.MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.testflashfile.SecondActivity"
            android:screenOrientation="portrait" >
        </activity>
        <activity
            android:name="com.example.testflashfile.ThirdActivity"
            android:screenOrientation="portrait" >
        </activity>
        <activity
            android:name="com.example.testflashfile.FourthActivity"
            android:screenOrientation="portrait" >
        </activity>
        <activity
            android:name="com.example.testflashfile.FifthActivity"
            android:screenOrientation="portrait" >
        </activity>
        <activity
            android:name="com.example.testflashfile.ReadText"
            android:screenOrientation="portrait" >
        </activity>
        <activity
            android:name="com.example.testflashfile.PlayAnimationActivity"
            android:screenOrientation="portrait" >
        </activity>
    </application>

</manifest>

日志猫:

03-13 11:01:47.792: D/AndroidRuntime(630): Shutting down VM
03-13 11:01:47.843: W/dalvikvm(630): threadid=1: thread exiting with uncaught exception (group=0x40015560)
03-13 11:01:47.972: E/AndroidRuntime(630): FATAL EXCEPTION: main
03-13 11:01:47.972: E/AndroidRuntime(630): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.testflashfile/com.example.testflashfile.MainActivity}: java.lang.NullPointerException
03-13 11:01:47.972: E/AndroidRuntime(630):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
03-13 11:01:47.972: E/AndroidRuntime(630):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-13 11:01:47.972: E/AndroidRuntime(630):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-13 11:01:47.972: E/AndroidRuntime(630):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-13 11:01:47.972: E/AndroidRuntime(630):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-13 11:01:47.972: E/AndroidRuntime(630):  at android.os.Looper.loop(Looper.java:123)
03-13 11:01:47.972: E/AndroidRuntime(630):  at android.app.ActivityThread.main(ActivityThread.java:3683)
03-13 11:01:47.972: E/AndroidRuntime(630):  at java.lang.reflect.Method.invokeNative(Native Method)
03-13 11:01:47.972: E/AndroidRuntime(630):  at java.lang.reflect.Method.invoke(Method.java:507)
03-13 11:01:47.972: E/AndroidRuntime(630):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-13 11:01:47.972: E/AndroidRuntime(630):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-13 11:01:47.972: E/AndroidRuntime(630):  at dalvik.system.NativeStart.main(Native Method)
03-13 11:01:47.972: E/AndroidRuntime(630): Caused by: java.lang.NullPointerException
03-13 11:01:47.972: E/AndroidRuntime(630):  at android.content.ContextWrapper.getResources(ContextWrapper.java:80)
03-13 11:01:47.972: E/AndroidRuntime(630):  at com.example.testflashfile.ReadText.readTxt(ReadText.java:24)
03-13 11:01:47.972: E/AndroidRuntime(630):  at com.example.testflashfile.ReadText.<init>(ReadText.java:14)
03-13 11:01:47.972: E/AndroidRuntime(630):  at com.example.testflashfile.MainActivity.<init>(MainActivity.java:26)
03-13 11:01:47.972: E/AndroidRuntime(630):  at java.lang.Class.newInstanceImpl(Native Method)
03-13 11:01:47.972: E/AndroidRuntime(630):  at java.lang.Class.newInstance(Class.java:1409)
03-13 11:01:47.972: E/AndroidRuntime(630):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
03-13 11:01:47.972: E/AndroidRuntime(630):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
03-13 11:01:47.972: E/AndroidRuntime(630):  ... 11 more
03-13 11:01:52.054: I/Process(630): Sending signal. PID: 630 SIG: 9

我已经尝试过的事情:1.清单中提到了类ReadText。2.在相关问题中检查了关于堆栈溢出的类似线程。

请注意:问题出在第 41 行:

helloTxt.setText(readText.readTxt());

对此问题的任何帮助/建议/指针表示赞赏。

4

3 回答 3

1

答案:(感谢@ρяσѕρєя K)

readText=new ReadText(this);应该在活动的 onCreate 方法中。

这解决了我的问题。我仍然想知道为什么在 onCreate 中定义它是必不可少的。

谢谢。

于 2013-03-13T05:58:15.697 回答
0

我认为你忘记在 public ReadText(Context c) 中传递参数意味着你应该在这里传递一些东西作为你的方法 public String readTxt() ,它不应该是空白的

public class ReadText {
Context context;

公共读取文本(上下文 c)

{
    context = c;
    readTxt();
}

公共字符串 readTxt()

{
    InputStream inputStream = context.getResources().openRawResource(R.raw.textone);

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

    int i;
    try {
        i = inputStream.read();
        while (i != -1)
        {
            byteArrayOutputStream.write(i);
            i = inputStream.read();
        }

        inputStream.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return byteArrayOutputStream.toString();
} 
于 2013-03-13T05:57:59.560 回答
0

我认为 context.getResources() 正在返回 null。这是在活动的上下文中吗?如果不是,getResources 将返回 null。

换句话说,请确保 this 关键字是您认为的那样。

于 2013-03-13T05:47:45.707 回答