gpt4 book ai didi

java - Android 错误尝试在空对象引用上调用虚拟方法

转载 作者:太空宇宙 更新时间:2023-11-04 13:27:23 25 4
gpt4 key购买 nike

我是 Android Studio 的新手(并且讨厌从 Eclipse 的过渡体验)。我正在尝试编写一个当前有两个 Activity 的应用程序。我已经编写了主要逻辑的 Java 代码,并且运行良好,但尚未将其放入第二个 Activity 中。当我尝试编写并运行该应用程序时,它给了我一个错误。

有一个 LoginActivity 和 MainActivity。目前,LoginActivity 只有一个虚拟文本和一个将用户带到 MainActivity 的按钮。目前 MainActivity 中只有一个 TextView。当我单击它时,应用程序崩溃并给出以下错误。我可以看到它与启动 MainActivity 有关,但我似乎无法修复它。

09-10 20:16:43.012  21126-21126/com.projects.example.myapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.projects.example.myapp, PID: 21126
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.projects.example.myapp/com.projects.example.myapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2418)
at android.app.ActivityThread.access$900(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5289)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.Activity.findViewById(Activity.java:2072)
at com.projects.example.myapp.MainActivity.<init>(MainActivity.java:22)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1606)
at android.app.Instrumentation.newActivity(Instrumentation.java:1066)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2418)
            at android.app.ActivityThread.access$900(ActivityThread.java:154)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5289)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

我的 xml 代码:

<LinearLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".LoginActivity"
android:orientation="vertical" >

<TextView
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<Button
android:id="@+id/loginbtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:onClick="goToMainActivity" />

</LinearLayout>

LoginActivity相关代码:

public void goToMainActivity(View view)
{
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
}

请求的MainActivity代码(第22行由前缀**表示):

public class MainActivity extends AppCompatActivity {

String currentWinnerName = "";

**TextView currentWinnerLabel = (TextView)findViewById(R.id.currentWinnerLabel);**

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

currentWinnerLabel.setText("Finding...");
//try
//{
//checkCurrentWinner(); //will implement this

//}
//catch (Exception e)
//{
//print e.getMessage() to log
//}

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

最佳答案

原因是,您试图在创建 Activity 之前为 currentWinnerLabel 赋值。

除非创建了 Activity,否则上下文为 null,并且您将在 null 对象引用上调用 findViewById 方法。

只需更改您的代码:

TextView currentWinnerLabel;

及之后:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

currentWinnerLabel = (TextView)findViewById(R.id.currentWinnerLabel);

...

关于java - Android 错误尝试在空对象引用上调用虚拟方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32513625/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com