2013-03-13 61 views
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.在清单中提到了class ReadText。 2.在相关问题中检查堆栈溢出的类似线程。

请注意:这个问题是行号41:

helloTxt.setText(readText.readTxt()); 

任何帮助/建议/写指针对这个问题表示赞赏。

+3

move'readText = new ReadText(this);'内部onCreate方法的活动 – 2013-03-13 05:44:29

+1

也见[Android阅读文本原始资源文件](http://stackoverflow.com/questions/4087674/android-read-text-raw资源文件)相同的问题 – 2013-03-13 05:47:54

+0

谢谢ton @ρяσѕρєяK。你能告诉我这是如何工作的。在onCreate之外写入有什么错误? 再次非常感谢。 – 2013-03-13 05:48:49

回答

1

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

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

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

谢谢。

0

我认为context.getResources()返回null。这是在一个活动的背景下吗?如果没有,getResources将返回null。

换句话说,请确保这个关键字是你的想法。

+0

感谢回复@DjHacktorReborn。那么我不认为这是问题,虽然在构造函数中提到上下文是getReasources方法必不可少的。并且它不返回null。我从ρяσѕρєяK.得到了解决方案。 – 2013-03-13 05:53:32

0

我想你忘记传递参数公众的ReadText(上下文C)是指,你应该在这里为你的方法公共字符串readTxt()传递的东西,它不应该是空白

public class ReadText { 
Context context; 

公众的ReadText(上下文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(); 
} 
相关问题