错误:很不幸<;应用程序名称>;已在android emulator中停止
我正在开发登录页面,它显示错误,不幸的是LoginC已经停止工作。 logcat显示以下消息错误:很不幸<;应用程序名称>;已在android emulator中停止,android,android-fragments,nullpointerexception,android-xml,Android,Android Fragments,Nullpointerexception,Android Xml,我正在开发登录页面,它显示错误,不幸的是LoginC已经停止工作。 logcat显示以下消息 03-31 07:24:35.896: D/AndroidRuntime(1263): Shutting down VM 03-31 07:24:35.906: W/dalvikvm(1263): threadid=1: thread exiting with uncaught exception (group=0xb3a1bba8) 03-31 07:24:35.946: E/AndroidRunti
03-31 07:24:35.896: D/AndroidRuntime(1263): Shutting down VM
03-31 07:24:35.906: W/dalvikvm(1263): threadid=1: thread exiting with uncaught exception (group=0xb3a1bba8)
03-31 07:24:35.946: E/AndroidRuntime(1263): FATAL EXCEPTION: main
03-31 07:24:35.946: E/AndroidRuntime(1263): Process: com.example.loginc, PID: 1263
03-31 07:24:35.946: E/AndroidRuntime(1263): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.loginc/com.example.loginc.MainActivity}: java.lang.NullPointerException
03-31 07:24:35.946: E/AndroidRuntime(1263): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-31 07:24:35.946: E/AndroidRuntime(1263): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-31 07:24:35.946: E/AndroidRuntime(1263): at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-31 07:24:35.946: E/AndroidRuntime(1263): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-31 07:24:35.946: E/AndroidRuntime(1263): at android.os.Handler.dispatchMessage(Handler.java:102)
03-31 07:24:35.946: E/AndroidRuntime(1263): at android.os.Looper.loop(Looper.java:136)
03-31 07:24:35.946: E/AndroidRuntime(1263): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-31 07:24:35.946: E/AndroidRuntime(1263): at java.lang.reflect.Method.invokeNative(Native Method)
03-31 07:24:35.946: E/AndroidRuntime(1263): at java.lang.reflect.Method.invoke(Method.java:515)
03-31 07:24:35.946: E/AndroidRuntime(1263): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-31 07:24:35.946: E/AndroidRuntime(1263): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-31 07:24:35.946: E/AndroidRuntime(1263): at dalvik.system.NativeStart.main(Native Method)
03-31 07:24:35.946: E/AndroidRuntime(1263): Caused by: java.lang.NullPointerException
03-31 07:24:35.946: E/AndroidRuntime(1263): at com.example.loginc.MainActivity.onCreate(MainActivity.java:35)
03-31 07:24:35.946: E/AndroidRuntime(1263): at android.app.Activity.performCreate(Activity.java:5231)
03-31 07:24:35.946: E/AndroidRuntime(1263): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-31 07:24:35.946: E/AndroidRuntime(1263): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-31 07:24:35.946: E/AndroidRuntime(1263): ... 11 more
MainActivity.java
public class MainActivity extends ActionBarActivity {
private EditText username=null;
private EditText password=null;
private TextView attempts;
private Button login;
int counter = 3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username = (EditText)findViewById(R.id.editText1);
password = (EditText)findViewById(R.id.editText2);
attempts = (TextView)findViewById(R.id.textView5);
attempts.setText(Integer.toString(counter));
login = (Button)findViewById(R.id.button1);
}
public void login(View view){
if(username.getText().toString().equals("admin") &&
password.getText().toString().equals("admin")){
Toast.makeText(getApplicationContext(), "Redirecting...",
Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getApplicationContext(), "Wrong Credentials",
Toast.LENGTH_SHORT).show();
attempts.setBackgroundColor(Color.RED);
counter--;
attempts.setText(Integer.toString(counter));
if(counter==0){
login.setEnabled(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;
}
}
}
以上是mainactivity.java文件中编写的代码
下面的详细信息写在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.loginc.MainActivity$PlaceholderFragment" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:text="@string/hello_world"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="50dp"
android:text="@string/username"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView2"
android:layout_marginLeft="32dp"
android:layout_toRightOf="@+id/textView2"
android:ems="10"
android:inputType="text" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:layout_marginTop="38dp"
android:text="@string/password"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView3"
android:layout_alignLeft="@+id/editText1"
android:ems="10"
android:inputType="textPassword" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText2"
android:layout_centerHorizontal="true"
android:layout_marginTop="94dp"
android:onClick="login"
android:text="@string/Login" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_below="@+id/textView3"
android:layout_marginLeft="30dp"
android:layout_marginTop="48dp"
android:text="@string/attempts"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView1"
android:layout_alignTop="@+id/textView4"
android:text="TextView" />
</RelativeLayout>
空点异常表示您试图对设置为
Null
(基本上未初始化)的值执行操作。你可以想象一个没有设置的值,比如一张未编程的信用卡,你可以随身携带,但如果你试图使用它,它就不会起作用。那么,在onCreate
函数中,什么值仍然可以为空
我强烈怀疑第35行就是这个,如果不是,那么这个答案就不适用了。您的错误在第35行,或者至少与此相关:
attempts.setText(Integer.toString(counter));
我假设尝试
在某种程度上是空的。这套在哪里
attempts = (TextView)findViewById(R.id.textView5);
好的,让我们看一下
公共视图findViewById(内部id)
在API级别1中添加
从创建的XML中查找由id属性标识的视图
已在onCreate(Bundle)中处理
返回:如果找到,则返回视图;否则返回null
因此,如果无法以某种方式找到该视图,则尝试可能为空。我们怎么知道它是否真的为空?让我们尝试一个log语句,在findViewById
Log.v("Main","attempts="+attempts);
好的,让我们假设它实际上是空值。那我们怎么办?根据文件,这意味着它没有找到。好,让我们看一下setContentView语句:
setContentView(R.layout.activity_main);
好的,那么您的视图应该在activity\u main.xml
中。我看到它在您的代码中,但是您调用了文件fragment\u main.xml
。底线是,您试图从错误的XML文件中获取尝试
值。好的,那么这里的解决方案是什么
fragment\u main.xml
的片段中管理所有这些内容。这可能需要一些工作,但最终会给你最好的表现setContentView
中替换R.layout.fragment\u main
如果选择第一个,则需要将代码放入
onCreateView
中,并通过根视图引用(onCreateView返回的内容)。类似于view.findViewById的内容
将它们移动到onCreateView
username = (EditText)findViewById(R.id.editText1);
password = (EditText)findViewById(R.id.editText2);
attempts = (TextView)findViewById(R.id.textView5);
attempts.setText(Integer.toString(counter));
login = (Button)findViewById(R.id.button1);
由于这些id位于fragment\u main.xml
中,而不在activity\u main.xml
中,因此NPE
另外,在那里使用
rootView.findViewById
请将Java代码与布局XML一起发布。也发布MainActivity.javaPost您的活动\u main.XML。错误日志告诉您错误所在:MainActivity.Java类第35行。首先看一下这一行。将所有视图移动到片段。观点属于碎片我明白你想说的。。如果我选择第一个选项,那么需要做哪些更改?好的……您能告诉我fragment_main.xml和activity_main.xml之间的区别吗。这两个文件的确切用途是什么?