gpt4 book ai didi

java - 带有 NullPointerException 的新 onClickListener (Android)

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

我在运行时使用 onClickListeners 创建了一些 textView。

但是我得到了一个NullPointer..。我认为原因是View,他不知道textView应该在哪里。

我该如何解决这个问题?

ShowSomething 类中有趣的几行代码:

textView.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

Toast.makeText(ShowSomething.this, textView.getText().toString(), Toast.LENGTH_LONG).show();
}
});
layout.addView(t);

完整类ShowSomething:

public class ShowSomething extends Activity {

public void showData(final String[][] array, final int i, int j, LinearLayout layout, final TextView t, TextView t2, int color) {
String Text = null;
int size = 12;
int top = 0;
int bot = 0;

if (j == 0) {
size = 14;
Text = array[j][i];
top = 5;
bot = 0;

}
if (j == 1) {
size = 10;
Text = " " + array[j][i];
top = 5;
bot = 5;
}
t.setTextSize(TypedValue.COMPLEX_UNIT_DIP, size);
t.setTextColor(color);
t.setPadding(0, top, 0, bot);
t.setClickable(false);
t.setText(Text);
t.setId(j + i);
t.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

Toast.makeText(ShowSomething.this, t.getText().toString(), Toast.LENGTH_LONG).show();
}
});
layout.addView(t);
}
}

ProjectsActivity中有趣的几行:

    setContentView(R.layout.activity_projects);
int j = 0;
//result
for (i = 0; i < array[0].length; i++) {
while (j <2) {
LinearLayout layout = (LinearLayout) findViewById(R.id.mainLayout);
final TextView t = new TextView(getApplicationContext());
final TextView t2 = new TextView(getApplicationContext());
ShowSomething.showData(array, i, j, layout, t, t2, color);

完整类ProjectsActivity:

public class ProjectsActivity extends Activity {
public String[][] array = null;
public int i = 0;
int color;
ShowSomething show = new ShowSomething();
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_projects);

int j = 0;

array = Connect.printJobList();

for (i = 0; i < array[0].length; i++) {
while (j < 2) {
LinearLayout layout = (LinearLayout) findViewById(R.id.mainLayout);
final TextView t = new TextView(getApplicationContext());
final TextView t2 = new TextView(getApplicationContext());

if (j == 0){
color = getResources().getColor(R.color.Project1);
}
if (j == 1){
color = getResources().getColor(R.color.Project2);
}

show.showData(array, i, j, layout, t, t2, color);
if (j == 1){
t2.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 1));
t2.setBackgroundColor(getResources().getColor(R.color.Line));
layout.addView(t2);
}
j++;
}
j = 0;
}
}

}

LogCat:

03-26 08:19:38.294: D/AndroidRuntime(1991): Shutting down VM
03-26 08:19:38.294: W/dalvikvm(1991): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
03-26 08:19:38.332: E/AndroidRuntime(1991): FATAL EXCEPTION: main
03-26 08:19:38.332: E/AndroidRuntime(1991): java.lang.NullPointerException
03-26 08:19:38.332: E/AndroidRuntime(1991): at android.app.Activity.setContentView(Activity.java:1895)
03-26 08:19:38.332: E/AndroidRuntime(1991): at de.xcom.mobilenav.ShowSomething$1.onClick(ShowSomething.java:47)
03-26 08:19:38.332: E/AndroidRuntime(1991): at android.view.View.performClick(View.java:4240)
03-26 08:19:38.332: E/AndroidRuntime(1991): at android.view.View$PerformClick.run(View.java:17721)
03-26 08:19:38.332: E/AndroidRuntime(1991): at android.os.Handler.handleCallback(Handler.java:730)
03-26 08:19:38.332: E/AndroidRuntime(1991): at android.os.Handler.dispatchMessage(Handler.java:92)
03-26 08:19:38.332: E/AndroidRuntime(1991): at android.os.Looper.loop(Looper.java:137)
03-26 08:19:38.332: E/AndroidRuntime(1991): at android.app.ActivityThread.main(ActivityThread.java:5103)
03-26 08:19:38.332: E/AndroidRuntime(1991): at java.lang.reflect.Method.invokeNative(Native Method)
03-26 08:19:38.332: E/AndroidRuntime(1991): at java.lang.reflect.Method.invoke(Method.java:525)
03-26 08:19:38.332: E/AndroidRuntime(1991): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-26 08:19:38.332: E/AndroidRuntime(1991): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-26 08:19:38.332: E/AndroidRuntime(1991): at dalvik.system.NativeStart.main(Native Method)

ShowSomething第47行:(忽略注释)

43 public void onClick(View v) { 
44 //Intent intent = new Intent(ShowSomething.this, ProjectDetailActivity.class);
45 //intent.putExtra("Project", array[0][i]);
46
47 //startActivity(intent);
48
49 Toast.makeText(ShowSomething.this, t.getText().toString(), Toast.LENGTH_LONG).show();

更改了类ShowSomething:

public class ShowSomething {
.
.
. t.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent(ShowSomething.this, ProjectDetailActivity.class);
intent.putExtra("Project", array[0][i]);

startActivity(intent);

//Toast.makeText(getApplicationContext() , t.getText().toString(), Toast.LENGTH_LONG).show();
}
});

但现在我收到一个错误:构造函数 Intent(ShowSomething, Class) 未定义...

最佳答案

ShowSomething show = new ShowSomething();

您无法使用new实例化 Activity 。

看起来 ShowSomething 根本不应该是一个 Activity 。例如,您可以将 Context 作为方法参数传递给它。

要将数据从一个 Activity 传递到另一个 Activity ,请使用 Intent 。请参阅How do I pass data between Activities in Android application?

关于java - 带有 NullPointerException 的新 onClickListener (Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22661538/

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