我正在为我的项目构建一个移动应用程序,我必须在其上检索发送到Thingspeak的数据。
当我点击图像按钮时,它应该打开一个新活动,该活动将显示从Thingspeak检索到的最后一个值。
我得到的问题是,当我点击图像按钮时应用程序停止工作,该图像按钮应该将我带到另一个活动。它确实打开了一项新的活动,显示导航到另一项活动正在运行,但随后显示"智能农业不幸停止工作"。我认为问题出在活动的xml
个文件或我TextView
文件中使用的TempHumidity.java
,但我真的不知道如何解决它们是绝对的Android Studio中的初学者。我有5个文件,即activity_main.xml
,MainActivity.java
,TempHumidity.java
,AndroidManifest.xml
,imgbtnmenu.xml
TempHumidity.java
类(它包含HttpURLConnection代码)
public class TempHumidity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.imgbtnmenu);
new GetMethodDemo().execute("https://thingspeak.com/channels/357670/field/1/last");
}
public class GetMethodDemo extends AsyncTask<String , Void ,String> {
String server_response;
private TextView aTextView;
@Override
protected String doInBackground(String... strings) {
URL url;
HttpURLConnection urlConnection = null;
try {
url = new URL(strings[0]);
urlConnection = (HttpURLConnection) url.openConnection();
int responseCode = urlConnection.getResponseCode();
if(responseCode == HttpURLConnection.HTTP_OK){
server_response = readStream(urlConnection.getInputStream());
Log.v("CatalogClient", server_response);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Log.e("Response", "" + server_response);
aTextView.setText(s);
}
}
// Converting InputStream to String
private String readStream(InputStream in) {
BufferedReader reader = null;
StringBuffer response = new StringBuffer();
try {
reader = new BufferedReader(new InputStreamReader(in));
String line = "";
while ((line = reader.readLine()) != null) {
response.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return response.toString();
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.teerna.smartagriculture">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".TempHumidity"> // you need to add these 3 lines inside application tag.
</activity>
</application>
</manifest>
imgbtnmenu.xml
用于新活动菜单(点击湿度和温度图像按钮时打开的菜单)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
答案 0 :(得分:0)
大多数logcat只是警告,你可以看到W/
字符。该应用程序崩溃是因为您正在收到java.lang.VerifyError
Could not find class 'android.graphics.drawable.RippleDrawable
。此错误仅发生在pre-lolipop设备(Android 5.0)
尝试将这些行添加到build.gradle:
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
如果仍无效,请将每个ImageView中的app:srcCompat
更改为android:src