以下是报错信息:
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.helloworld/android.widget.TextView}; have you declared this activity in your AndroidManifest.xml?
然后我搜索了一下得到的解决方案是没有将activity在mainfest.xml中注册,但是我已经注册过了,以下是xml中的代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.helloworld">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".TextViewActivity"
android:label=""></activity>//注册第二个活动
<activity android:name=".MainActivity">
//前面的点是包名省略,
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
以下是第二个活动的代码
package com.example.helloworld;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Paint;
import android.os.Bundle;
import android.widget.TextView;
public class TextViewActivity extends AppCompatActivity {
private TextView tv2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text_view);
tv2 = findViewById(R.id.tv2);
tv2.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);//加入划线
tv2.getPaint().setAntiAlias(true);//去除锯齿
}
}
以下是第二个活动的xml代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tv_test1"
android:textColor="#000000"
android:textSize="30sp"
/>//在strings.xml中编辑好后,在这里调用
//最大行数为一,且显示不下时出现省略号
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:maxLines="1"
android:ellipsize="end"
android:id="@+id/t2"
android:text="哎哟不错哦!"
android:textColor="#000000"
android:textSize="30sp"/>
//有额外选项或者下拉框(大致这个意思)
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:drawableRight="@drawable/arrow3"
android:text="for you and me"
android:textColor="#000000"
android:textSize="30sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="xxx"
android:id="@+id/tv2"
android:textColor="#000000"
android:textSize="30sp"
/>
</LinearLayout>
以下是mainactivity的代码(之后补充的)
public class MainActivity extends AppCompatActivity {
private Button btntv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);//设置内容视图
btntv = findViewById(R.id.bt_tv1);
btntv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//跳转到TextView演示界面
Intent intent = new Intent(MainActivity.this, TextView.class);
startActivity(intent);
}
});
}
}