在activity_main.xml中,我创建了一个textview、edittext和button。
活动\u main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:layout_width="50dp"
android:layout_height="20dp"
android:layout_marginTop="250dp"
android:id="@+id/edit"
android:inputType="phone"
android:textColor="@color/white"
app:layout_constraintBottom_toTopOf="@id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter the number"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="@+id/edit"
app:layout_constraintEnd_toStartOf="@+id/edit"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:text="click here"
android:onClick="VehicleNum"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
当我点击按钮时,它将调用“vehiclenum”方法。在这里,我通过intent调用numberracticity。在设备中运行代码后,当我单击按钮时,应用程序停止工作。帮帮我。
主活动.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void VehicleNum(View view){
Intent intent = new Intent(this, NumberActivity.class);
startActivity(intent);
}
}
列表视图.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvAnimals"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
数字.java
public class Number {
private Integer mNum;
public Number(Integer nNum){
mNum = nNum;
}
public Integer getmNum(){
return mNum;
}
}
numberactivity.java文件
public class NumberActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_view);
ArrayList<Number> gokul = new ArrayList<>();
int low = 0, high = 999;
int num;
int res;
EditText edit = (EditText)findViewById(R.id.edit);
int val = Integer.parseInt( edit.getText().toString() );
for (int number = low; number <= high; ++number) {
num = remind(number);
if (num > 9) {
res = remind(num);
if (res == val) {
gokul.add(new Number(number));
}
} else {
if (num == val) {
gokul.add(new Number(number));
}
}
}NumberAdapter adapter = new NumberAdapter(this,gokul);
ListView listView = (ListView) findViewById(R.id.listview_number);
listView.setAdapter(adapter);
}
public int remind(int num){
int result = 0;
while (num != 0) {
int remainder = num % 10;
result += remainder;
num /= 10;
}
return result;
}
}
列表项.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="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:minHeight="?attr/listPreferredItemHeight"
android:padding="16dp">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text=" "/>
</LinearLayout>
numberadapter.java文件
public class NumberAdapter extends ArrayAdapter<Number> {
private static final String LOG_TAG = NumberAdapter.class.getSimpleName();
public NumberAdapter(Activity context, ArrayList<Number> gokul) {
super(context,0,gokul );
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if(listItemView==null){
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item,parent,false);
}
Number currentNumberActivty = getItem(position);
TextView miwokTextView = (TextView) listItemView.findViewById(R.id.textView);
miwokTextView.setText(currentNumberActivty.getmNum());
return listItemView;
}
}
logcat公司
2021-01-02 16:15:55.971 9161-9161/com.example.recyclerview e/androidruntime:fatal exception:main进程:com.example.recyclerview,pid:9161 java.lang.illegalstateexception:无法执行的方法android:onclick at androidx.appcompat.app.appcompatviewinflater$declaredonclicklistener.onclick(appcompatviewinflater。java:414)在android.view.view.performclick(view。java:6608)在com.google.android.material.button.materialbutton.performclick(materialbutton。java:992)在android.view.view.performclickinternal(视图。java:6585)在android.view.view.access$3100(查看。java:785)在android.view.view$performclick.run(view。java:25921)在android.os.handler.handlecallback(handler。java:873)在android.os.handler.dispatchmessage(handler。java:99)在android.os.looper.loop(looper。java:201)在android.app.activitythread.main(活动线程。java:6810)位于com.android.internal.os.runtimeinit$methodAndArgscaler.run(runtimeinit)的java.lang.reflect.method.invoke(本机方法)。java:547)在com.android.internal.os.zygoteinit.main(zygoteinit。java:873)原因:java.lang.reflect.invocationtargetexception atjava.lang.reflect.method.invoke(本机方法)位于androidx.appcompat.app.appcompatviewinflater$declaredonclicklistener.onclick(appcompatviewinflater)。java:409)在android.view.view.performclick(view。java:6608) 在com.google.android.material.button.materialbutton.performclick(materialbutton。java:992) 在android.view.view.performclickinternal(视图。java:6585) 在android.view.view.access$3100(查看。java:785) 在android.view.view$performclick.run(view。java:25921) 在android.os.handler.handlecallback(handler。java:873) 在android.os.handler.dispatchmessage(handler。java:99) 在android.os.looper.loop(looper。java:201) 在android.app.activitythread.main(activitythread。java:6810) 位于java.lang.reflect.method.invoke(本机方法) 在com.android.internal.os.runtimeinit$methodandargscaller.run(runtimeinit。java:547) 在com.android.internal.os.zygoteinit.main(zygoteinit。java:873) 原因:android.content.activitynotfoundexception:找不到显式活动类{com.example.recyclerview/com.example.recyclerview.number};您是否在androidmanifest.xml中声明了此活动?在android.app.instrumentation.checkstartactivityresult(instrumentation。java:2020)在android.app.instrumentation.execstartactivity(instrumentation。java:1679)在android.app.activity.startactivityforresult(activity。java:4651)在androidx.fragment.app.fragmentactivity.startactivityforresult(fragmentactivity。java:676)在android.app.activity.startactivityforresult(活动。java:4609)在androidx.fragment.app.fragmentactivity.startactivityforresult(fragmentactivity。java:663)在android.app.activity.startactivity(activity。java:4970)在android.app.activity.startactivity(activity。java:4938)在com.example.recyclerview.mainactivity.vehiclenum(mainactivity。java:27)在java.lang.reflect.method.invoke(本机方法) 在androidx.appcompat.app.appcompatviewinflater$declaredonclicklistener.onclick(appcompatviewinflater。java:409) 在android.view.view.performclick(view。java:6608) 在com.google.android.material.button.materialbutton.performclick(materialbutton。java:992) 在android.view.view.performclickinternal(视图。java:6585) 在android.view.view.access$3100(查看。java:785) 在android.view.view$performclick.run(view。java:25921) 在android.os.handler.handlecallback(handler。java:873) 在android.os.handler.dispatchmessage(handler。java:99) 在android.os.looper.loop(looper。java:201) 在android.app.activitythread.main(activitythread。java:6810) 位于java.lang.reflect.method.invoke(本机方法) 在com.android.internal.os.runtimeinit$methodandargscaller.run(runtimeinit。java:547) 在com.android.internal.os.zygoteinit.main(zygoteinit。java:873) 2021-01-02 16:15:56.018 9161-9161/com.example.recyclerview i/process:发送信号。pid:9161信号:9
暂无答案!
目前还没有任何答案,快来回答吧!