我有一个问题,如果有人有帮助,我会很高兴!
这是崩溃日志:
FATAL EXCEPTION: main
Process: com.elvigo.swedencup.vigor, PID: 29477
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.content.ContextWrapper.getResources(ContextWrapper.java:90)
at android.view.ContextThemeWrapper.getResourcesInternal(ContextThemeWrapper.java:127)
at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:121)
at android.support.v7.app.AppCompatActivity.getResources(AppCompatActivity.java:549)
at android.view.View.<init>(View.java:4551)
at android.view.View.<init>(View.java:4694)
at android.view.ViewGroup.<init>(ViewGroup.java:597)
at android.widget.LinearLayout.<init>(LinearLayout.java:234)
at android.widget.LinearLayout.<init>(LinearLayout.java:230)
at android.widget.LinearLayout.<init>(LinearLayout.java:226)
at android.widget.LinearLayout.<init>(LinearLayout.java:222)
at android.widget.TableRow.<init>(TableRow.java:60)
at com.elvigo.swedencup.vigor.TeamScoreTableUI.addRows(TeamScoreTableUI.java:78)
at com.elvigo.swedencup.vigor.TeamScoreTableUI.CalladdRows(TeamScoreTableUI.java:65)
at com.elvigo.swedencup.vigor.Classes.MatchTableDataFeatching.onPostExecute(MatchTableDataFeatching.java:139)
at com.elvigo.swedencup.vigor.Classes.MatchTableDataFeatching.onPostExecute(MatchTableDataFeatching.java:26)
at android.os.AsyncTask.finish(AsyncTask.java:695)
at android.os.AsyncTask.-wrap1(Unknown Source:0)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:712)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6809)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
活动包括被叫方法:
public class TeamScoreTableUI extends AppCompatActivity {
public static TextView TextBox;
FloatingActionButton PrevScreenBtn;
public static TableLayout ll;
public static TextView textView;
public static TableRow row;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.teamscoring);
MatchTableDataFeatching process = new MatchTableDataFeatching();
process.execute();
TextBox = (TextView) findViewById(R.id.TextBox);
PrevScreenBtn = (FloatingActionButton) findViewById(R.id.BackButton);
PrevScreenBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
Context context = getApplicationContext();
Toast toast = Toast.makeText(context, "FairPlayCup", Toast.LENGTH_SHORT);
toast.show();
ll = (TableLayout) findViewById(R.id.TableLayout);
}
public void CalladdRows(){
addRows();
}
public void addRows() {
//for (int i = 0; i < 2; i++) {
Log.d("dataFeatchingDone", "done");
row = new TableRow(this);
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
row.setLayoutParams(lp);
TextView textView = new TextView(this);
textView.setText("10");
row.addView(textView);
ll.addView(row, 1);
//}
}
}
我还在调试模式下测试了应用程序,我发现它崩溃了:row = new TableRow(this);
我正在调用“CalladdRows()”方法。
我用以下方法调用方法:
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
Log.d("app", "onPostExecute");
TeamScoreTableUI.TextBox.setText(this.dataParsed);
TeamScoreTableUI TeamScoreTableUIAddRows = new TeamScoreTableUI();
TeamScoreTableUIAddRows.CalladdRows();
}
该方法在TeamScoreTableUI中,名为CalladdRows ..
如果有人能抽出时间和帮助我会很高兴!
答案 0 :(得分:0)
代码
TeamScoreTableUI.TextBox.setText(this.dataParsed);
TeamScoreTableUI TeamScoreTableUIAddRows = new TeamScoreTableUI();
TeamScoreTableUIAddRows.CalladdRows();
您正在错误地创建一个新活动,并在这个新创建的活动上调用“CalladdRows()”(而不是使用与您用来设置“TextBox”的活动相同的活动。
尝试将代码更改为
TeamScoreTableUI.TextBox.setText(this.dataParsed);
TeamScoreTableUI.CalladdRows()
作为附注
您的字段和方法名称中的大写案例使代码难以阅读。我有问题弄清楚asyncTask中的TeamScoreTableUI / TextBox是字段还是静态类。
通常在JAVA中,你使用小写字母(camelCase)来表示字段,变量和方法名称;和班级的资本(PascalCase)。
答案 1 :(得分:0)
尝试进行这些更改:
1. Pass context to your Task
MatchTableDataFeatching process = new MatchTableDataFeatching(this);
2. Get context in your Task
Context context;
public MatchTableDataFeatching(Context c) {
context = c;
}
.......
3. Then in your onPost
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
((TeamScoreTableUI)context).TextBox.setText(this.dataParsed);
((TeamScoreTableUI)context).CalladdRows();
}