Android程序没有报错,怎么总是弹出“很抱歉,‘XXXX’已停止运行”

lux_music 2014-12-21 02:00:23
logcat内容:

12-21 13:46:48.452: E/AndroidRuntime(1207): FATAL EXCEPTION: main
12-21 13:46:48.452: E/AndroidRuntime(1207): java.lang.RuntimeException: Unable to start activity ComponentInfo{lux.aust.android_sample_file2/lux.aust.android_sample_file2.MainActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x7f030000
12-21 13:46:48.452: E/AndroidRuntime(1207): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-21 13:46:48.452: E/AndroidRuntime(1207): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-21 13:46:48.452: E/AndroidRuntime(1207): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-21 13:46:48.452: E/AndroidRuntime(1207): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-21 13:46:48.452: E/AndroidRuntime(1207): at android.os.Handler.dispatchMessage(Handler.java:99)
12-21 13:46:48.452: E/AndroidRuntime(1207): at android.os.Looper.loop(Looper.java:137)
12-21 13:46:48.452: E/AndroidRuntime(1207): at android.app.ActivityThread.main(ActivityThread.java:5041)
12-21 13:46:48.452: E/AndroidRuntime(1207): at java.lang.reflect.Method.invokeNative(Native Method)
12-21 13:46:48.452: E/AndroidRuntime(1207): at java.lang.reflect.Method.invoke(Method.java:511)
12-21 13:46:48.452: E/AndroidRuntime(1207): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-21 13:46:48.452: E/AndroidRuntime(1207): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-21 13:46:48.452: E/AndroidRuntime(1207): at dalvik.system.NativeStart.main(Native Method)
12-21 13:46:48.452: E/AndroidRuntime(1207): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f030000
12-21 13:46:48.452: E/AndroidRuntime(1207): at android.content.res.Resources.getValue(Resources.java:1014)
12-21 13:46:48.452: E/AndroidRuntime(1207): at android.content.res.Resources.loadXmlResourceParser(Resources.java:2139)
12-21 13:46:48.452: E/AndroidRuntime(1207): at android.content.res.Resources.getLayout(Resources.java:853)
12-21 13:46:48.452: E/AndroidRuntime(1207): at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
12-21 13:46:48.452: E/AndroidRuntime(1207): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
12-21 13:46:48.452: E/AndroidRuntime(1207): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
12-21 13:46:48.452: E/AndroidRuntime(1207): at android.app.Activity.setContentView(Activity.java:1881)
12-21 13:46:48.452: E/AndroidRuntime(1207): at lux.aust.android_sample_file2.MainActivity.onCreate(MainActivity.java:22)
12-21 13:46:48.452: E/AndroidRuntime(1207): at android.app.Activity.performCreate(Activity.java:5104)
12-21 13:46:48.452: E/AndroidRuntime(1207): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-21 13:46:48.452: E/AndroidRuntime(1207): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-21 13:46:48.452: E/AndroidRuntime(1207): ... 11 more

求高手解惑!
...全文
5493 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
叹了口丶气 2017-01-10
  • 打赏
  • 举报
回复
和你遇到一样的问题。。在空指针那个地方加个try catch 可以过
peter_nj_RD 2015-05-04
  • 打赏
  • 举报
回复
引用 8 楼 u012743061 的回复:
setContentView之前没有加上这行代码:super.onCreate(savedInstanceState);
踩屎3号 2015-05-04
  • 打赏
  • 举报
回复
setContentView之前没有加上这行代码:super.onCreate(savedInstanceState);
dielianxian 2014-12-29
  • 打赏
  • 举报
回复
Debug 跟踪 tv1.setText(getFromRaw("test1.txt")); tv2.setText(getFromAsset("test2.txt")); 这两个方法里 方法体里应该获取到异常了
-图- 2014-12-29
  • 打赏
  • 举报
回复
没找到资源文件 getResources().getAssets().open("test2.txt");
Fuuqiu 2014-12-28
  • 打赏
  • 举报
回复
空指针异常?是不是没有注册activity或者控件引入错误
lux_music 2014-12-27
  • 打赏
  • 举报
回复
引用 3 楼 u010683904 的回复:
2楼正解
怎么改啊
追着蜗牛喷跑 2014-12-22
  • 打赏
  • 举报
回复
2楼正解
lux_music 2014-12-22
  • 打赏
  • 举报
回复
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tv1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/tv2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>
package lux.aust.android_sample_file2;

import java.io.IOException;
import java.io.InputStream;

import org.apache.http.util.EncodingUtils;

import android.app.Activity;
import android.content.res.Resources.NotFoundException;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends Activity {
	public static final String ENCODING = "UTF-8";
	TextView tv1;
	TextView tv2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_main);
		tv1 = (TextView) findViewById(R.id.tv1);
		tv2 = (TextView) findViewById(R.id.tv2);

		tv1.setText(getFromRaw("test1.txt"));
		tv2.setText(getFromAsset("test2.txt"));

	}

	public String getFromAsset(String string) {
		String result = "";
		InputStream in = null;
		try {
			in = getResources().getAssets().open("test2.txt");
			int length = in.available();
			byte[] buffer = new byte[length];
			in.read(buffer);
			result = EncodingUtils.getString(buffer, ENCODING);
		} catch (NotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}

	public String getFromRaw(String string) {
		String result = "";
		InputStream in = null;
		try {
			in = getResources().openRawResource(R.raw.test1);
			int length = in.available();
			byte[] buffer = new byte[length];
			in.read(buffer);
			result = EncodingUtils.getString(buffer, ENCODING);
		} catch (NotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		return result;
	}

	@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);
	}
}
heaven_wsh 2014-12-21
  • 打赏
  • 举报
回复
at lux.aust.android_sample_file2.MainActivity.onCreate(MainActivity.java:22) 你的MainActivity 22行包的错,看log应该是setContentView 把你的layout的代码贴出来看看,应该是layout离引用了一个应用不到的资源

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧