大家好,这是最近两天第二次遇到此问题。所以我想代码中有些地方出了个大问题。
无论如何,这里要做的只是在另一个类中调用一个方法,该方法将在我的活动类中显示特定的文本文件。
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());
任何帮助/建议/指针写给这个问题的表示赞赏。
最佳答案
回答:
(由于@ρяσsρєяK)readText=new ReadText(this);
应该在活动的onCreate方法内。
那解决了我的问题。我仍然想知道为什么在onCreate内定义此必不可少。
谢谢。
关于android - 无法启动Activity componentinfo,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15377875/