gpt4 book ai didi

java - 致命异常 : main (School Project)

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

这个问题在这里已经有了答案:





What is a NullPointerException, and how do I fix it?

(12 个回答)


6年前关闭。




我正在为我的类(class)做一个期末项目,一切进展顺利,然后我尝试运行该应用程序,在我的手机上崩溃了:

04-29 13:36:24.185 3507-3507/com.example.jessewmoore.database2 E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.jessewmoore.database2, PID: 3507 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jessewmoore.database2/com.example.jessewmoore.database2.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3184) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3294) at android.app.ActivityThread.access$1000(ActivityThread.java:210) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1704) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:6938) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116) at org.json.JSONTokener.nextValue(JSONTokener.java:94) at org.json.JSONArray.(JSONArray.java:92) at org.json.JSONArray.(JSONArray.java:108) at com.example.jessewmoore.database2.MainActivity.onCreate(MainActivity.java:72) at android.app.Activity.performCreate(Activity.java:6575) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1134) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3137) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3294)  at android.app.ActivityThread.access$1000(ActivityThread.java:210)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1704)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:145)  at android.app.ActivityThread.main(ActivityThread.java:6938)  at java.lang.reflect.Method.invoke(Native Method)  at java.lang.reflect.Method.invoke(Method.java:372)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 



每件事都应该是好的,没有错误。

Activity 主
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:focusable="true">

<TableLayout android:id="@+id/table"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:focusableInTouchMode="true"
android:focusable="true"></TableLayout>

</HorizontalScrollView>
</ScrollView>

AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.jessewmoore.database2">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

主要 Activity
package com.example.jessewmoore.database2;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.TableRow.LayoutParams;

public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

String result = null;
InputStream is = null;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http: busdata.pe.hu/myfile.php");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();

Log.e("log_tag", "connection_success");
// Toast.makeText(getApplicationContext(), “pass”, Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection" + e.toString());
Toast.makeText(getApplicationContext(), "Connection fail", Toast.LENGTH_SHORT).show();

}
//convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "ISO-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
// Toast.makeText(getApplicationContext(), “Input Reading pass”, Toast.LENGTH_SHORT).show();
}
is.close();

result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting results" + e.toString());
Toast.makeText(getApplicationContext(), "Input reading fail", Toast.LENGTH_SHORT).show();

}

//parse json data
try {
JSONArray jArray = new JSONArray(result);
TableLayout tv = (TableLayout) findViewById(R.id.table);
tv.removeAllViewsInLayout();
int flag = 1;
for (int i = -1; i < jArray.length() - 1; i++) {
TableRow tr = new TableRow(MainActivity.this);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
if (flag == 1) {
TextView b6 = new TextView(MainActivity.this);
b6.setText("Id");
b6.setTextColor(Color.BLUE);
b6.setTextSize(15);
tr.addView(b6);
TextView b19 = new TextView(MainActivity.this);
b19.setPadding(10, 0, 0, 0);
b19.setTextSize(15);
b19.setText("Name");
b19.setTextColor(Color.BLUE);
tr.addView(b19);
TextView b29 = new TextView(MainActivity.this);
b29.setPadding(10, 0, 0, 0);
b29.setText("Status");
b29.setTextColor(Color.BLUE);
b29.setTextSize(15);
tr.addView(b29);
tv.addView(tr);
final View vline = new View(MainActivity.this);
vline.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 2));
vline.setBackgroundColor(Color.BLUE);
tv.addView(vline);
flag = 0;
} else {
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag", "id: " + json_data.getInt("Id") + ", Username: " + json_data.getString("username") + ", No:" + json_data.getString("comment"));
TextView b = new TextView(MainActivity.this);
String stime = String.valueOf(json_data.getInt("Id"));
b.setText(stime);
b.setTextColor(Color.RED);
b.setTextSize(15);
tr.addView(b);
TextView b1 = new TextView(MainActivity.this);
b1.setPadding(10, 0, 0, 0);
b1.setTextSize(15);
String stime1 = json_data.getString("username");
b1.setText(stime1);
b1.setTextColor(Color.BLACK);
tr.addView(b1);
TextView b2 = new TextView(MainActivity.this);
b2.setPadding(10, 0, 0, 0);
String stime2 = json_data.getString("comment");
b2.setText(stime2);
b2.setTextColor(Color.BLACK);
b2.setTextSize(15);
tr.addView(b2);
tv.addView(tr);
final View vline1 = new View(MainActivity.this);
vline1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
vline1.setBackgroundColor(Color.WHITE);
tv.addView(vline1);
}
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data" + e.toString());
Toast.makeText(getApplicationContext(), "JsonArray_fail", Toast.LENGTH_SHORT).show();
}

}

}

最佳答案

您的错误是 NullPointerException,由 MainActivity.java 中的第 72 行引起,即:

JSONArray jArray = new JSONArray(result);

为了进一步了解您的错误,JSONArray 似乎试图在结果上调用 length(),可能是在它试图从字符串构造适当的 JSON 数组时。能够理解这些错误对于解决您的问题很重要,并且可以帮助您提出更好的问题!

您最初将结果设置为 null - 在调用新 JSONArray(result) 之前似乎没有正确设置。一个好的开始是检查直接在它之前初始化结果的 try block 是否正确完成并且没有失败。在“将响应转换为字符串”try block 上的 catch 语句出现错误之前,您的日志中可能有一些内容。

关于java - 致命异常 : main (School Project),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36945477/

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