gpt4 book ai didi

android - 无法启动 Activity 组件信息

转载 作者:行者123 更新时间:2023-11-30 03:43:33 24 4
gpt4 key购买 nike

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

主要 Activity 是:

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

list 文件是:

<?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. list 中提到了 ReadText 类。2.检查了相关问题中堆栈溢出的类似线程。

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

helloTxt.setText(readText.readTxt());

感谢任何对此问题的帮助/建议/指点。

最佳答案

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

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

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

谢谢。

关于android - 无法启动 Activity 组件信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15377875/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com