我有一个更新我的eclipse和sdk到一个更新的版本。现在的证据是当我创建一个新的android项目而不是只有1个java和1个xml文件时我也创建了1个片段。我的问题是为什么我在appcompat_v7中收到错误。这是因为appcompat_v7,因为我的代码没有错误,为什么我无法运行应用程序。我不知道为什么我有这个android.content.ContextWrapper.getResources(ContextWrapper.java:89)
,因为我的代码最多只有87行。
04-07 04:36:57.490: E/AndroidRuntime(1176): FATAL EXCEPTION: main
04-07 04:36:57.490: E/AndroidRuntime(1176): Process: com.afield.numpad, PID: 1176
04-07 04:36:57.490: E/AndroidRuntime(1176): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.afield.numpad/com.afield.numpad.MainActivity}: java.lang.NullPointerException
04-07 04:36:57.490: E/AndroidRuntime(1176): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
04-07 04:36:57.490: E/AndroidRuntime(1176): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-07 04:36:57.490: E/AndroidRuntime(1176): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-07 04:36:57.490: E/AndroidRuntime(1176): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-07 04:36:57.490: E/AndroidRuntime(1176): at android.os.Handler.dispatchMessage(Handler.java:102)
04-07 04:36:57.490: E/AndroidRuntime(1176): at android.os.Looper.loop(Looper.java:136)
04-07 04:36:57.490: E/AndroidRuntime(1176): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-07 04:36:57.490: E/AndroidRuntime(1176): at java.lang.reflect.Method.invokeNative(Native Method)
04-07 04:36:57.490: E/AndroidRuntime(1176): at java.lang.reflect.Method.invoke(Method.java:515)
04-07 04:36:57.490: E/AndroidRuntime(1176): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-07 04:36:57.490: E/AndroidRuntime(1176): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-07 04:36:57.490: E/AndroidRuntime(1176): at dalvik.system.NativeStart.main(Native Method)
04-07 04:36:57.490: E/AndroidRuntime(1176): Caused by: java.lang.NullPointerException
04-07 04:36:57.490: E/AndroidRuntime(1176): at android.content.ContextWrapper.getResources(ContextWrapper.java:89)
04-07 04:36:57.490: E/AndroidRuntime(1176): at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78)
04-07 04:36:57.490: E/AndroidRuntime(1176): at com.afield.numpad.MainActivity.<init>(MainActivity.java:25)
04-07 04:36:57.490: E/AndroidRuntime(1176): at java.lang.Class.newInstanceImpl(Native Method)
04-07 04:36:57.490: E/AndroidRuntime(1176): at java.lang.Class.newInstance(Class.java:1208)
04-07 04:36:57.490: E/AndroidRuntime(1176): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
04-07 04:36:57.490: E/AndroidRuntime(1176): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
04-07 04:36:57.490: E/AndroidRuntime(1176): ... 11 more
MainActivity
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.Toast;
import android.os.Build;
import android.app.Activity;
import android.widget.AdapterView;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
GridView gridView;
final String[] numbers = new String[] {
"1", "2", "3", "4", "5",
"6", "7", "8", "9", "0",getResources().getString(R.string.dialogc)};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView) findViewById(R.id.gridView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, numbers);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(getApplicationContext(),
((TextView) v).getText(), Toast.LENGTH_SHORT).show();
}
});
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
@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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
我的错误是numpad文件夹和appcompat_v7文件夹中的红色感叹号
更新logcat
04-07 05:08:33.670: E/AndroidRuntime(1279): FATAL EXCEPTION: main
04-07 05:08:33.670: E/AndroidRuntime(1279): Process: com.afield.numpad, PID: 1279
04-07 05:08:33.670: E/AndroidRuntime(1279): java.lang.NoClassDefFoundError: android.support.v7.appcompat.R$styleable
04-07 05:08:33.670: E/AndroidRuntime(1279): at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:104)
04-07 05:08:33.670: E/AndroidRuntime(1279): at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:57)
04-07 05:08:33.670: E/AndroidRuntime(1279): at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98)
04-07 05:08:33.670: E/AndroidRuntime(1279): at com.afield.numpad.MainActivity.onCreate(MainActivity.java:18)
04-07 05:08:33.670: E/AndroidRuntime(1279): at android.app.Activity.performCreate(Activity.java:5231)
04-07 05:08:33.670: E/AndroidRuntime(1279): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-07 05:08:33.670: E/AndroidRuntime(1279): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-07 05:08:33.670: E/AndroidRuntime(1279): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-07 05:08:33.670: E/AndroidRuntime(1279): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-07 05:08:33.670: E/AndroidRuntime(1279): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-07 05:08:33.670: E/AndroidRuntime(1279): at android.os.Handler.dispatchMessage(Handler.java:102)
04-07 05:08:33.670: E/AndroidRuntime(1279): at android.os.Looper.loop(Looper.java:136)
04-07 05:08:33.670: E/AndroidRuntime(1279): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-07 05:08:33.670: E/AndroidRuntime(1279): at java.lang.reflect.Method.invokeNative(Native Method)
04-07 05:08:33.670: E/AndroidRuntime(1279): at java.lang.reflect.Method.invoke(Method.java:515)
04-07 05:08:33.670: E/AndroidRuntime(1279): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-07 05:08:33.670: E/AndroidRuntime(1279): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-07 05:08:33.670: E/AndroidRuntime(1279): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
引起:java.lang.NullPointerException 04-07 04:36:57.490: E / AndroidRuntime(1176):at android.content.ContextWrapper.getResources(ContextWrapper.java:89)
您正在使用此
getResources().getString(R.string.dialogc)
在onCreate
之外。将其移至onCreate
。
http://developer.android.com/reference/android/content/Context.html#getResources()
public abstract Resources getResources ()
Added in API level 1
Return a Resources instance for your application's package.
getResources()
是Context的一种方法,一旦创建了Activity,就可以使用上下文。
String[] numbers;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
numbers = new String[] {
"1", "2", "3", "4", "5",
"6", "7", "8", "9", "0",getResources().getString(R.string.dialogc)};
编辑:
java.lang.NoClassDefFoundError: android.support.v7.appcompat.R $设置样式
您尚未正确引用AppCompat。因此问题
选中使用资源添加库。
https://developer.android.com/tools/support-library/setup.html
并按照相应的步骤进行操作
答案 1 :(得分:2)
在numbers
方法中初始化onCreate()
数组,如下所示......
String[] numbers;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
numbers = new String[] {
"1", "2", "3", "4", "5",
"6", "7", "8", "9", "0",getResources().getString(R.string.dialogc)};
}
答案 2 :(得分:0)
在eclipse中创建每个新项目时,它还会创建一个appcompat_v7。必须存在以运行代码。请检查它是否存在。
答案 3 :(得分:0)
getResources()。getString(R.string.dialogc)这一行应该在onCreate()
中答案 4 :(得分:0)
试试这个:
在活动中;
this.getResources().getString(R.string.dialogc);
外部活动;
contextObject.getResources().getString(R.string.dialogc);
像这样:
String[] numbers;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
numbers = new String[] {
"1", "2", "3", "4", "5",
"6", "7", "8", "9", "0",this.getResources().getString(R.string.dialogc)};
}
答案 5 :(得分:0)
您是否更新了支持库
Goto Window >> Android SDK Manager >> Extras >> Android Support Library
更新它
以及getResources().getString(R.string.dialogc)
此行应位于onCreate()