gpt4 book ai didi

java - 实例化 Activity 时出错

转载 作者:太空宇宙 更新时间:2023-11-04 14:23:46 25 4
gpt4 key购买 nike

我已经解决了这个问题,我检查了我的 list 文件,它告诉我我的 Activity 不再具体,我错误地将类更改为抽象,将其从抽象改回修复了错误

我在我的应用程序中添加了一个界面,这样我就可以根据 ListView 中的按钮单击来编辑文本编辑,在我添加到此界面之前,该应用程序运行良好,现在当我尝试运行该应用程序时出现错误。我搜索了论坛,但找到了解决我的问题的方法,希望有人能提供帮助!

这是我的主要 Activity

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.pinchtapzoom.R;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.SQLException;

public abstract class MyActivity extends Activity implements MyAdapterInterface{

private CustomCursorAdapter customAdapter;
public ListView list1;

com.example.rory.dbtest.DBAdapter db = new com.example.rory.dbtest.DBAdapter(this);

//public EditText TotalCost;
//public EditText TotalLitres;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);

list1 = (ListView)findViewById(R.id.data_list);
db.open();


final MyAdapterInterface ad = new MyAdapterInterface() {
@Override
public void updateEditText(int value) {
EditText myEditText = (EditText)findViewById(R.id.edit1);
myEditText.setText(value);
}
};


Button addBtn = (Button)findViewById(R.id.add);
addBtn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent i = new Intent(MyActivity.this, addassignment.class);
startActivity(i);
}
});

Button deleteBtn = (Button)findViewById(R.id.delete);
deleteBtn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent i = new Intent(MyActivity.this, Delete.class);
startActivity(i);
}
});

Button updateBtn = (Button)findViewById(R.id.update);
updateBtn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent i = new Intent(MyActivity.this, Update.class);
startActivity(i);
}
});


try {
String destPath = "/data/data/" + getPackageName() + "/databases/AssignmentDB";
File f = new File(destPath);
if (!f.exists()) {
CopyDB( getBaseContext().getAssets().open("mydb"),
new FileOutputStream(destPath));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


new Handler().post(new Runnable() {
@Override
public void run() {
//Log.d("test", "customadapter is " + customAdapter.toString());
//Log.d("test", "databaseHelper is " + databaseHelper.toString());
customAdapter = new CustomCursorAdapter(MyActivity.this, db.getAllRecords(), ad);
list1.setAdapter(customAdapter);
}
});




/* @Override
public void updateEditText(String value) {
EditText myEditText = (EditText)findViewById(R.id.edit1);
myEditText.setText(value);
}*/


}

private class DBAdapter extends BaseAdapter {
private LayoutInflater mInflater;
//private ArrayList<>

@Override
public int getCount() {

return 0;
}

@Override
public Object getItem(int arg0) {

return null;
}

@Override
public long getItemId(int arg0) {

return 0;
}

@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {

return null;
}

}

public void CopyDB(InputStream inputStream, OutputStream outputStream)
throws IOException {
//---copy 1K bytes at a time---
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
inputStream.close();
outputStream.close();
}
}

这是我的适配器类

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.pinchtapzoom.R;


public class CustomCursorAdapter extends CursorAdapter{

public int counter = 0;
private MyAdapterInterface mMyInterface;

public CustomCursorAdapter(Context context, Cursor c, MyAdapterInterface myInterface) {

super(context, c);
this.context = context;
this.mMyInterface = myInterface;
}
Context context;


@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// when the view will be created for first time,
// we need to tell the adapters, how each item will look
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View retView = inflater.inflate(R.layout.row, parent, false);

return retView;
}

public void bindView(View view, Context context, Cursor cursor) {
// here we are setting our data
// that means, take the data from the cursor and put it in views
TextView textViewPersonName = (TextView) view.findViewById(R.id.item1);
textViewPersonName.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(1))));

final EditText edit1 = (EditText) view.findViewById(R.id.runningTotal);

Button plusButton = (Button) view.findViewById(R.id.plusButton);
plusButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
counter++;
edit1.setText(Integer.toString(counter));
mMyInterface.updateEditText(counter);
}
});

final Button minusButton = (Button) view.findViewById(R.id.minusButton);
minusButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
counter--;
edit1.setText(Integer.toString(counter));
mMyInterface.updateEditText(counter);
}
});
}
}

这是我的界面

public interface MyAdapterInterface {

public void updateEditText(int value);
}

这是我收到的 logcat 错误

    Process: com.example.rory.dbtest, PID: 10950
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.rory.dbtest/com.example.rory.dbtest.MyActivity}: java.lang.InstantiationException: can't instantiate class com.example.rory.dbtest.MyActivity
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.InstantiationException: can't instantiate class com.example.rory.dbtest.MyActivity
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1208)
at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2101)

            

最佳答案

final MyAdapterInterface ad = new MyAdapterInterface() {
@Override
public void updateEditText(int value) {
EditText myEditText = (EditText)findViewById(R.id.edit1);
myEditText.setText(value);
}
};

不应该发生在 onCreate() 方法内部,而应该发生在类的其他地方,作为普通方法。应该这样写:

public void updateEditText(int value) {
EditText myEditText = (EditText)findViewById(R.id.edit1);
myEditText.setText(value);
}

关于java - 实例化 Activity 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26891707/

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