/ *我没有对代码进行任何更改。以前它运行正常。我不知道它为什么在执行时显示运行时异常。请帮助* /
package com.example.klarity;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
public abstract class Screen1 extends ActionBarActivity implements OnItemSelectedListener{
Spinner practiceSpin;
Spinner projectSpin;
JSONObject projDet;
JSONArray projArr;
ArrayList<String> arrData=new ArrayList<String>();
ArrayList<String> arrPrac=new ArrayList<String>();
int[] project;
URL url;
BufferedReader br;
String selected1;
/*sprivate final static String JSON_DATA =
"{"
+ " \"practice\": ["
+ " {"
+ " \"name\": \"autosar\""
+ " },"
+ " {"
+ " \"name\": \"powertrain\""
+ " }," //JSONArray for PRACTICES created
+ " {"
+ " \"name\": \"JSONArray-2\""
+ " },"
+ " {"
+ " \"name\": \"JSON-3\""
+ " }"
+ " ]"
+ "}"; */
private final static String JSON_DATA2 =
"{"
+ " \"project\": ["
+ " {"
+ " \"name\": \"Project-1\""
+ " },"
+ " {"
+ " \"name\": \"Project-2\""
+ " }," //JSONArray for PROJECTS created
+ " {"
+ " \"name\": \"Project-3\""
+ " },"
+ " {"
+ " \"name\": \"Project-4\""
+ " }"
+ " ]"
+ "}";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen1);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
ConnectionHelper con = new ConnectionHelper();
String response = con.doInBackground(); //readUrl will be copied in response.
String tokens[] = response.split("\\*");
/* //PRACTICE ARRAY CODE
try {
projDet=new JSONObject(JSON_DATA);
projArr=projDet.getJSONArray("practice");
for(int n = 0; n < projArr.length(); n++)
{
JSONObject projDet = projArr.getJSONObject(n);
arrPrac.add(projArr.getJSONObject(n).getString("name"));
}
} catch (JSONException e) {
e.printStackTrace();
System.out.println(e.getMessage());
System.out.println("exception occured");
} */
try {
practiceSpin=(Spinner) findViewById(R.id.spinnerPractice);
ArrayAdapter<String> adapter_proj1 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,tokens); //"The constructor ArrayAdapter<String>(MainActivity, int, int, JSONArray)
adapter_proj1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
practiceSpin.setAdapter(adapter_proj1);
practiceSpin.setOnItemSelectedListener(this);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
System.out.println(e1.getMessage());
System.out.println("exception occured in 2nd catch");
}
/* //PROJECT ARRAY CODE
try {
projDet=new JSONObject(JSON_DATA2);
projArr=projDet.getJSONArray("project");
for(int n = 0; n < projArr.length(); n++)
{
JSONObject projDet = projArr.getJSONObject(n);
arrData.add(projArr.getJSONObject(n).getString("name"));
}
} catch (JSONException e) {
e.printStackTrace();
System.out.println(e.getMessage());
System.out.println("exception occured");
}
try {
//PROJECT ARRAY ADAPTER
projectSpin=(Spinner) findViewById(R.id.spinnerProject);
ArrayAdapter<String> adapter_proj = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,arrData); //"The constructor ArrayAdapter<String>(MainActivity, int, int, JSONArray)
adapter_proj.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
projectSpin.setAdapter(adapter_proj);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
System.out.println(e1.getMessage());
System.out.println("exception occured in 2nd catch");
} */
}
public void onItemSelected(android.widget.AdapterView<?> parent, View view, int position, long id) {
String selected1 = parent.getItemAtPosition(position).toString();
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.screen1, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private class ConnectionHelper extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
try {
url = new URL("http://192.168.1.34/aditya/getAllPractices.php");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
br=new BufferedReader(new InputStreamReader(url.openStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String readUrl = "";
try {
readUrl=br.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return readUrl;
}
}
}
这就是logCat显示的内容:
02-19 19:15:40.467: E/AndroidRuntime(1487): FATAL EXCEPTION: main
02-19 19:15:40.467: E/AndroidRuntime(1487): Process: com.example.klarity, PID: 1487
02-19 19:15:40.467: E/AndroidRuntime(1487): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.klarity/com.example.klarity.Screen1}: java.lang.InstantiationException: class com.example.klarity.Screen1 cannot be instantiated
02-19 19:15:40.467: E/AndroidRuntime(1487): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
02-19 19:15:40.467: E/AndroidRuntime(1487): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
02-19 19:15:40.467: E/AndroidRuntime(1487): at android.app.ActivityThread.access$800(ActivityThread.java:144)
02-19 19:15:40.467: E/AndroidRuntime(1487): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
02-19 19:15:40.467: E/AndroidRuntime(1487): at android.os.Handler.dispatchMessage(Handler.java:102)
02-19 19:15:40.467: E/AndroidRuntime(1487): at android.os.Looper.loop(Looper.java:135)
02-19 19:15:40.467: E/AndroidRuntime(1487): at android.app.ActivityThread.main(ActivityThread.java:5221)
02-19 19:15:40.467: E/AndroidRuntime(1487): at java.lang.reflect.Method.invoke(Native Method)
02-19 19:15:40.467: E/AndroidRuntime(1487): at java.lang.reflect.Method.invoke(Method.java:372)
02-19 19:15:40.467: E/AndroidRuntime(1487): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
02-19 19:15:40.467: E/AndroidRuntime(1487): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
02-19 19:15:40.467: E/AndroidRuntime(1487): Caused by: java.lang.InstantiationException: class com.example.klarity.Screen1 cannot be instantiated
02-19 19:15:40.467: E/AndroidRuntime(1487): at java.lang.Class.newInstance(Class.java:1553)
02-19 19:15:40.467: E/AndroidRuntime(1487): at android.app.Instrumentation.newActivity(Instrumentation.java:1065)
02-19 19:15:40.467: E/AndroidRuntime(1487): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2199)
02-19 19:15:40.467: E/AndroidRuntime(1487): ... 10 more
/ *这是棘手的问题。没有对代码进行任何更改。我以前跑得很厉害。
答案 0 :(得分:2)
您不能将抽象类本身用作活动,您必须具有具体实现(扩展此抽象类)或删除抽象关键字。