gpt4 book ai didi

java - 当我点击 Android 应用程序中的 "Notes"按钮时,如何调用我的 NoteMain 类

转载 作者:行者123 更新时间:2023-12-01 13:03:03 25 4
gpt4 key购买 nike

如上所述。我确定我做错了什么。这是 MainActivity.java 的代码和 NoteMain.java 的代码 当我在应用程序加载时单击主屏幕上的“Notes”按钮时,它显示 不幸的是,(应用程序名称)已停止

不知道为什么?如有任何帮助,我们将不胜感激。

提前致谢

主要 Activity :

package com.qub.buildersbuddy;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

Button buttonConverter;
Button buttonCalculator;
Button buttonNotePad;



@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);



Button ConvertBtn = (Button) findViewById(R.id.butonConverter);
ConvertBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,CentInch.class);
startActivity(intent);

}
});


Button CalcBtn = (Button) findViewById(R.id.buttonCalc);
CalcBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,Calculator.class);
startActivity(intent);

}
});

Button NoteBtn = (Button) findViewById(R.id.buttonNotes);
NoteBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,NoteMain.class);
startActivity(intent);

}
});
}

public void setupConverterButton(){
buttonConverter = (Button) findViewById(R.id.butonConverter);
// Button messageButton = (Button) findViewById(R.id.butonConverter);

}

public void CentToInch(){
buttonConverter.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
//opening the
try{
Class centClass = Class
.forName("com.qub.buildersbuddy.CentInch");
Intent myintent = new Intent(MainActivity.this,centClass);
startActivity(myintent);
}catch (ClassNotFoundException e){
e.printStackTrace();

}

}
});

}


public void setupCalculatorButton(){
buttonCalculator = (Button) findViewById(R.id.buttonCalc);
// Button messageButton = (Button) findViewById(R.id.butonConverter);

}

public void Calculator(){
buttonCalculator.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
//opening the
try{
Class calcClass = Class
.forName("com.qub.buildersbuddy.Calculator");
Intent myintent = new Intent(MainActivity.this,calcClass);
startActivity(myintent);
}catch (ClassNotFoundException e){
e.printStackTrace();

}

}
});

}

public void setupNotesButton(){
buttonNotePad = (Button) findViewById(R.id.buttonNotes);
// Button messageButton = (Button) findViewById(R.id.butonConverter);

}

public void Notes(){
buttonNotePad.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
//opening the
try{
Class NoteMain = Class
.forName("com.qub.buildersbuddy.NoteMain");
Intent myintent = new Intent(MainActivity.this,NoteMain);
startActivity(myintent);
}catch (ClassNotFoundException e){
e.printStackTrace();

}

}
});

}

}

注释主

package com.qub.buildersbuddy;


import java.util.ArrayList;
import java.util.Map;
import java.util.Map.Entry;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;

public class NoteMain extends Activity implements OnItemClickListener, OnClickListener {

private SharedPreferences spNotes;
private ArrayList<Note> notes;
private Button addNote;

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

this.addNote = (Button) findViewById(R.id.addNoteButton);
this.addNote.setOnClickListener(this);
spNotes = getSharedPreferences("notes", Context.MODE_PRIVATE);

}

@Override
protected void onResume() {
super.onResume();
showNotes();
ListView list = (ListView) findViewById(R.id.listNotes);
CustomAdapter aa = new CustomAdapter(this, getLayoutInflater(), this.notes, spNotes);
list.setAdapter(aa);
list.setOnItemClickListener(this);
}

private void showNotes() {
this.notes = new ArrayList<Note>();
Map map = spNotes.getAll();
for (Object o : map.entrySet()) {
Entry e = (Entry<String, String>) o;
this.notes.add(new Note((String)e.getKey(), (String)e.getValue()));
}
}

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
Intent intent = new Intent(this, NoteActivity.class);
intent.putExtra("title", this.notes.get(position).getTitle());
intent.putExtra("text", this.notes.get(position).getText());
startActivity(intent);
}


@Override
public void onClick(View arg0) {
Intent intent = new Intent(this, NoteActivity.class);
startActivity(intent);
}

}

AndroidManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.qub.buildersbuddy"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.qub.buildersbuddy.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.qub.buildersbuddy.CentInch"
android:label="@string/title_activity_cent_inch" >
</activity>
<activity
android:name="com.qub.buildersbuddy.Calculator"
android:label="@string/title_activity_calculator" >
</activity>
<activity
android:name="com.qub.buildersbuddy.NotePad"
android:label="@string/title_activity_note" >
</activity>



</application>

</manifest>

最佳答案

这可能是因为您没有在 list 中定义 NoteMain Activity 。

<activity
android:name="com.qub.buildersbuddy.NoteMain"
android:label="@string/title_activity_note" >
</activity>

关于java - 当我点击 Android 应用程序中的 "Notes"按钮时,如何调用我的 NoteMain 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23410096/

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