gpt4 book ai didi

java - 为什么我的应用程序 "unfortunately stop".我想要的只是一些简单的按钮

转载 作者:行者123 更新时间:2023-12-01 14:02:24 26 4
gpt4 key购买 nike

这是我的第一个应用程序。我所需要的只是五个按钮,其中两个用于调用某些电话号码(到目前为止只创建了一个),三个按钮用于将用户带到某个 URL。我没有收到任何错误或警告,也没有任何有关如何导航 LogCat 的说明。

日志猫:

10-08 14:41:40.716: D/AndroidRuntime(793): Shutting down VM
10-08 14:41:40.716: W/dalvikvm(793): threadid=1: thread exiting with uncaught exception (group=0x41465700)
10-08 14:41:40.777: E/AndroidRuntime(793): FATAL EXCEPTION: main
10-08 14:41:40.777: E/AndroidRuntime(793): java.lang.RuntimeException: Unable to start activity ComponentInfo{acps.mhs/acps.mhs.MainActivity}: java.lang.NullPointerException
10-08 14:41:40.777: E/AndroidRuntime(793): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
10-08 14:41:40.777: E/AndroidRuntime(793): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-08 14:41:40.777: E/AndroidRuntime(793): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-08 14:41:40.777: E/AndroidRuntime(793): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-08 14:41:40.777: E/AndroidRuntime(793): at android.os.Handler.dispatchMessage(Handler.java:99)
10-08 14:41:40.777: E/AndroidRuntime(793): at android.os.Looper.loop(Looper.java:137)
10-08 14:41:40.777: E/AndroidRuntime(793): at android.app.ActivityThread.main(ActivityThread.java:5103)
10-08 14:41:40.777: E/AndroidRuntime(793): at java.lang.reflect.Method.invokeNative(Native Method)
10-08 14:41:40.777: E/AndroidRuntime(793): at java.lang.reflect.Method.invoke(Method.java:525)
10-08 14:41:40.777: E/AndroidRuntime(793): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-08 14:41:40.777: E/AndroidRuntime(793): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-08 14:41:40.777: E/AndroidRuntime(793): at dalvik.system.NativeStart.main(Native Method)
10-08 14:41:40.777: E/AndroidRuntime(793): Caused by: java.lang.NullPointerException
10-08 14:41:40.777: E/AndroidRuntime(793): at acps.mhs.MainActivity.onCreate(MainActivity.java:25)
10-08 14:41:40.777: E/AndroidRuntime(793): at android.app.Activity.performCreate(Activity.java:5133)
10-08 14:41:40.777: E/AndroidRuntime(793): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-08 14:41:40.777: E/AndroidRuntime(793): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
10-08 14:41:40.777: E/AndroidRuntime(793): ... 11 more
10-08 14:42:19.696: I/Process(793): Sending signal. PID: 793 SIG: 9

Mainactivity.java

package acps.mhs;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity implements View.OnClickListener {

Button mhshome, pp, mhsdir, cmhs;

@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);


mhshome = (Button) findViewById(R.id.mhshome);
pp = (Button) findViewById(R.id.pp);
mhsdir = (Button) findViewById(R.id.mhsdir);
cmhs = (Button) findViewById(R.id.cmhs);

mhshome.setOnClickListener(this);
pp.setOnClickListener(this);
mhsdir.setOnClickListener(this);
cmhs.setOnClickListener(this);
}

@Override
public void onClick(View v) {
switch(v.getId()) {

case R.id.mhshome:
Uri uri = Uri.parse("http://www2.k12albemarle.org/school/mohs/Pages/default.aspx");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
break;


case R.id.pp:
Uri uri2 = Uri.parse("http://www2.k12albemarle.org/school/MOHS/Pages/Directory.aspx");
Intent intent2 = new Intent(Intent.ACTION_VIEW, uri2);
startActivity(intent2);
break;

case R.id.mhsdir:
Uri uri3 = Uri.parse("http://www2.k12albemarle.org/school/MOHS/Pages/Directory.aspx");
Intent intent3 = new Intent(Intent.ACTION_VIEW, uri3);
startActivity(intent3);
break;

case R.id.cmhs:
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:1234567890"));
startActivity(callIntent);
break;
}
}


@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;
}

}

activity_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=".MainActivity" >

<Button
android:id="@+id/pp"
style="@style/AppBaseTheme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/mhshome"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:text="@string/pp"/>

<Button
android:id="@+id/mhshome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="26dp"
android:onClick="onClick"
android:text="@string/mhshome" />

<Button
android:id="@+id/mhsdir"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/pp"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="@string/mhsdir" />

<Button
android:id="@+id/cmhs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/mhsdir"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="@string/callmhs" />

</RelativeLayout>

我确信这将是非常简单的事情。请为我指明正确的方向。

最佳答案

您需要调用 setContentView(R.layout.activity_main) :) 这就是 findViewById 在您的 View 上返回 null 的原因。

关于java - 为什么我的应用程序 "unfortunately stop".我想要的只是一些简单的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19256195/

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