我只是在学习Android编程时采取了一些步骤。我正在学习一些教程,在研究如何实现onKeyListener时,我收到一个错误' java.lang.RuntimeException:无法实例化活动ComponentInfo java .lang.NullPointerException&#39 ;.这个错误导致了另一个错误' adt,遗憾的是app已经停止了#39;试图在我的手机上运行该应用程序。这是logcat:
05-15 15:55:22.087: D/AndroidRuntime(20672): Shutting down VM
05-15 15:55:22.087: W/dalvikvm(20672): threadid=1: thread exiting with uncaught exception (group=0x41ea1ba8)
05-15 15:55:22.087: E/AndroidRuntime(20672): FATAL EXCEPTION: main
05-15 15:55:22.087: E/AndroidRuntime(20672): Process: com.example.keyboardlistener, PID: 20672
05-15 15:55:22.087: E/AndroidRuntime(20672): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.keyboardlistener/com.example.keyboardlistener.MainActivity}: java.lang.NullPointerException
感谢大家的回复。这是我目前的代码:
activity_main.xml中:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.keyboardlistener.MainActivity"
tools:ignore="MergeRootFrame" />
Fragment_Main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.keyboardlistener.MainActivity$PlaceholderFragment" >
<TextView
android:id="@+id/TextResults"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hi" />
<EditText
android:id="@+id/editTextUserEntry1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/TextResults"
android:layout_marginTop="58dp"
android:layout_toRightOf="@+id/TextResults"
android:ems="10" >
<requestFocus />
</EditText>
</RelativeLayout>
主要课程: 公共类MainActivity扩展ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}*/
final EditText ed = (EditText)findViewById(R.id.editTextUserEntry1);
final TextView tv = (TextView)findViewById(R.id.TextResults);
ed.setOnKeyListener(new OnKeyListener(){
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(event.getAction()==KeyEvent.ACTION_DOWN)
{
if(keyCode == KeyEvent.KEYCODE_ENTER)
{
tv.setText(ed.getText());
}
}
return false;
}
});
}
@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;
}
}
}
答案 0 :(得分:2)
Caused by: java.lang.NullPointerException
at android.app.Activity.findViewById(Activity.java:1884)
at com.example.keyboardlistener.MainActivity.<init>(MainActivity.java:18)
初始化findViewById()
成员变量时,您过早地调用MainActivity
。该活动还没有Window
。
在onCreate()
之后将初始化移至setContentView()
。在Window
阶段会有onCreate()
(没有这样的NPE)并假设视图位于您使用setContentView()
设置的布局中,您实际上可以在setContentView()
之后找到一些内容。如果视图位于片段布局中,请将初始化移至片段onCreateView()
(有关详细信息,请参阅NullPointerException accessing views in onCreate()。)
答案 1 :(得分:0)
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
您好像在这里使用Fragment
,这意味着所有触及用户界面的操作都应该在Fragment
内完成。
调用FragmentManager.beginTransaction()
是一个异步任务,这意味着当它完成时它不能保证Fragment
会被夸大,因此ed
可能是空的,导致ed.setOnKeyListener()
抛出NullPointerException
答案 2 :(得分:0)
退出此代码
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
因为您正在实现一个片段,所以您可以在主视图中找到gui组件,因为您的片段位于顶部。