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
求高手解惑!
10 个解决方案
#1
at lux.aust.android_sample_file2.MainActivity.onCreate(MainActivity.java:22)
你的MainActivity 22行包的错,看log应该是setContentView
把你的layout的代码贴出来看看,应该是layout离引用了一个应用不到的资源
你的MainActivity 22行包的错,看log应该是setContentView
把你的layout的代码贴出来看看,应该是layout离引用了一个应用不到的资源
#2
<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);
}
}
#3
2楼正解
#4
怎么改啊
#5
空指针异常?是不是没有注册activity或者控件引入错误
#6
没找到资源文件 getResources().getAssets().open("test2.txt");
#7
Debug 跟踪 tv1.setText(getFromRaw("test1.txt"));
tv2.setText(getFromAsset("test2.txt"));
这两个方法里
方法体里应该获取到异常了
tv2.setText(getFromAsset("test2.txt"));
这两个方法里
方法体里应该获取到异常了
#8
setContentView之前没有加上这行代码:super.onCreate(savedInstanceState);
#9
#10
和你遇到一样的问题。。在空指针那个地方加个try catch 可以过
#1
at lux.aust.android_sample_file2.MainActivity.onCreate(MainActivity.java:22)
你的MainActivity 22行包的错,看log应该是setContentView
把你的layout的代码贴出来看看,应该是layout离引用了一个应用不到的资源
你的MainActivity 22行包的错,看log应该是setContentView
把你的layout的代码贴出来看看,应该是layout离引用了一个应用不到的资源
#2
<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);
}
}
#3
2楼正解
#4
怎么改啊
#5
空指针异常?是不是没有注册activity或者控件引入错误
#6
没找到资源文件 getResources().getAssets().open("test2.txt");
#7
Debug 跟踪 tv1.setText(getFromRaw("test1.txt"));
tv2.setText(getFromAsset("test2.txt"));
这两个方法里
方法体里应该获取到异常了
tv2.setText(getFromAsset("test2.txt"));
这两个方法里
方法体里应该获取到异常了
#8
setContentView之前没有加上这行代码:super.onCreate(savedInstanceState);
#9
#10
和你遇到一样的问题。。在空指针那个地方加个try catch 可以过